首页 > 编程语言 > 详细

关于多线程Thread.Stop()破坏原子性

时间:2014-08-21 14:42:14      阅读:345      评论:0      收藏:0      [点我收藏+]
 
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
                MutiThread t=new MutiThread();
                Thread t1=new Thread(t);
                
                /*多线程断点调试,错误结果,如果在此处执行断点,t1执行完毕,numa回复正确状态-------此处注意*/
                t1.start();
                
                for(int i=0;i<5;i++){
                    new Thread(t).start();
                }
                t1.stop();
    }

}

public class MutiThread  implements Runnable {
    int numa=0;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        synchronized (this) {
            numa++;
            try{
                Thread.sleep(1000);
            }catch(Exception e){
                e.printStackTrace();
            }
            numa--;
            String stn=Thread.currentThread().getName();
            System.out.println(stn+"  numa= " + numa);
        }
    }
    
}

关于多线程Thread.Stop()破坏原子性,布布扣,bubuko.com

关于多线程Thread.Stop()破坏原子性

原文:http://www.cnblogs.com/whut-lp/p/3926989.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!