public static void main(String[] args) {
Tname th = new Tname();
new Thread(th).start();
new Thread(th).start();
new Thread(th).start();
new Thread(th).start();
}
}
class Tname extends Thread {
private static int i = 0;
public void run() {
while (true) {
synchronized (this) {
if (i >= 20) {
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
System.out.println(i);
// return;
System.out.println(Thread.currentThread().getName());
} //currentThread() 返回对当前正在执行的线程对象的引用。
}
}
}
|
|