当前位置: 代码迷 >> 编程 >> Java多线程编程#线程同步示范
  详细解决方案

Java多线程编程#线程同步示范

热度:10024   发布时间:2013-02-26 00:00:00.0
Java多线程编程#线程同步示例
1、线程不同步
在同一个进程中的线程都是独立运行的,线程之间会抢占cup,
执行先后顺序是不确定的,谁先得到谁就先执行

package com.boonya.multithread.explain;/**================================== * 文件:WhyThreadNotSynchronized.java   * 描述:多线程不同步的原因  * ================================== */  // 共享一个静态数据对象  public class WhyThreadNotSynchronized {	  	public static void main(String[] args) {		  MyTaskThread mythread1=new MyTaskThread("Thread1"); 		  MyTaskThread mythread2=new MyTaskThread("Thread2"); 		  mythread1.start();		  mythread2.start();	 }}class  MyTaskThread extends Thread{		public MyTaskThread(){}		public MyTaskThread(String threadName){	   super(threadName);    }	@Override	public void run() {		  // 为了更清楚地看到不正确的结果,这里放一个大的循环   	    for (int i = 0; i < 50; i++){   		       if (this.getName().equals("Thread1")){   		    	   Data.myData = "这是第 1 个线程";   		           // 为了演示产生的问题,这里设置一次睡眠   		           try{   		               Thread.sleep((int)Math.random() * 100);   		           }  catch(InterruptedException e){   		           }   		           // 输出结果   		           System.out.println(this.getName() + ":" +  Data.myData);   		       }else if (this.getName().equals("Thread2")){   		    	   Data.myData = "这是第 2个线程";   		           // 为了演示产生的问题,这里设置一次睡眠   		          try{   		              Thread.sleep((int)Math.random() * 100);   		          } catch(InterruptedException e){   		          }   		         // 输出结果   		         System.out.println(this.getName() + ":" + Data.myData);   		       }   		   }   	} }

2、synchronized 线程同步

package com.boonya.multithread.explain;import com.boonya.base.Data;/**==============================   * 文件:SynchronizedThread.java   * 描述:多线程不同步的解决方法--锁   * ============================== */  public class SynchronizedThread {		public static void main(String[] args) {		  MyTaskThread2 mythread1=new MyTaskThread2("Thread1"); 		  MyTaskThread2 mythread2=new MyTaskThread2("Thread2"); 		  mythread1.start();		  mythread2.start();	 }}class  MyTaskThread2 extends Thread{		public MyTaskThread2(){}		public MyTaskThread2(String threadName){	   super(threadName);    }	@Override	public void run() {		// 为了更清楚地看到不正确的结果,这里放一个大的循环   	    for (int i = 0; i < 50; i++){ 		       if (this.getName().equals("Thread1")){   		    	   // 锁定共享对象 		    	   synchronized (Data.myData){		    		   Data.myData=" is thread1 "+i;			           // 为了演示产生的问题,这里设置一次睡眠   			           try{   			               Thread.sleep((int)Math.random() * 100);   			           }  catch(InterruptedException e){   			           }   			           // 输出结果   			           System.out.println(this.getName() + ":----------thread 1 excuted" );   		    	   }		       }else if (this.getName().equals("Thread2")){   		    	   // 锁定共享对象 		    	   synchronized (Data.myData){		    		   Data.myData=" is thread2 "+i;			           // 为了演示产生的问题,这里设置一次睡眠   			          try{   			              Thread.sleep((int)Math.random() * 100);   			          } catch(InterruptedException e){			          }   			         // 输出结果   			         System.out.println(this.getName() + ":----------thread 2 excuted");   		    	   }		       }   		   }   	} }
  相关解决方案