/**
*
*/
package de.tutorials;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author Tom
*
*/
public class ThreadingExample {
/**
* @param args
*/
public static void main(String[] args) {
final Lock lock = new ReentrantLock(true);
Runnable runnable0 = new Runnable() {
public void run() {
for (int j = 0; j < 5; j++) {
try {
try {
lock.lock();
for (int i = 0; i < 10; i++) {
System.out.println(i);
TimeUnit.MILLISECONDS.sleep(250);
}
} finally {
lock.unlock();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
Runnable runnable1 = new Runnable() {
public void run() {
int i = 0;
while (i < 5) {
try {
try {
lock.lock();
System.out.println(System.nanoTime() + " BUBU");
i++;
} finally {
lock.unlock();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(runnable0);
executorService.execute(runnable1);
executorService.shutdown();
}
}