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()破坏原子性
时间: 2024-10-11 00:13:44