public class ThreadTest
{
private Object lock = new Object();
public ThreadTest(final String str, final int nSleep)
{
Thread syncThread = new Thread(new Runnable()
{
int num = 0;
public void run()
{
while (num < 20) {
try {
if (true) {
Thread.sleep(nSleep);
}
synchronized (lock) {
num++;
System.out.println(str + " The " + num + "th call in");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
});
syncThread.setName("My SyncOperation Thread");
syncThread.start();
}
public static void main(String[] args)
{
ThreadTest tt = new ThreadTest("a", 10000);
ThreadTest ttt = new ThreadTest("b", 1000);
}
}
阅读(1850) | 评论(0) | 转发(0) |