java多线程中的volatile和synchronized用法分析
本文实例分析了java多线程中的volatile和synchronized用法。分享给大家供大家参考。具体实现方法如下:
packagecom.chzhao;
publicclassVolatiletestextendsThread{
privatestaticintcount=0;
publicvoidrun(){ count++; }
publicstaticvoidmain(String[]args){ Threadthreads[]=newThread[10000]; for(inti=0;i<threads.length;i++){ threads[i]=newVolatiletest(); } for(inti=0;i<threads.length;i++){ threads[i].start(); } try{ Thread.sleep(1000); }catch(InterruptedExceptione){ e.printStackTrace(); } System.out.println(count); } }