/**
* Created by FireLang on 2017-07-18.
*/
public class TheardRunnable implements Runnable{
private static int count = 0;
@Override
public void run() {
synchronized (this){
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName()+"--->"+count++);
}
}
}
}
/**
* Created by FireLang on 2017-07-18.
*/
public class TestThreadRunnable {
public static void main(String[] args) {
TheardRunnable runnable = new TheardRunnable();
Thread t1 = new Thread(runnable,"T1");
Thread t2 = new Thread(runnable,"T2");
t1.start();
t2.start();
}
}
输出有两种情况:
T1在前和T2在前。你可以尝试运行!!!