多线程出现明显的抢占【ForYou】

来源:互联网 发布:齐鲁证券交易软件 编辑:程序博客网 时间:2024/05/21 10:48
/** * 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在前。你可以尝试运行!!!