這是我做的一個(gè)很簡單的多線程同步程序,目的是為了
測試多 線程編程下如何使用同步(synchronized)防止產(chǎn)生競爭共享資源的錯(cuò)誤狀態(tài),從中得到的心得是:一定要將你所共享的變量封裝在一個(gè)類中,將所有 有關(guān)該變量的操作方法都盡可能地封裝在包含該變量的類中,并將所有有關(guān)讀取修改該共享變量的方法都設(shè)為同步方法,只有這樣才是安全的,并且該變量必須是 private類型,主要是為了防止其他對象無意讀取到該變量而使該變量的同步形同虛設(shè)!因?yàn)槟憧梢圆煌ㄟ^同步方法直接對該共享變量進(jìn)行操作!不說了,下 面來看代碼吧!我還在代碼中加了一個(gè)計(jì)時(shí)器類Timer類,這個(gè)類可以產(chǎn)生一個(gè)后臺(tái)線程,專門用于計(jì)時(shí)到指定時(shí)間或延時(shí)一定時(shí)間就去執(zhí)行TimeTask 線程對象任務(wù)。
package xinyu.shangrao.demo.fucking;
import java.util.Date; import java.text.ParseException; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.TimeUnit;
public class ThreadDemoNew { public static void main(String[] args) throws ParseException { long counter; /* Date date = null; String s = "2013-05-29 上午08:26 "; SimpleDateFormat sdf = new SimpleDateFormat(); date = sdf.parse(s); System.out.println("------系統(tǒng)默認(rèn)無參數(shù)Date的parse------"); System.out.println(" " +date.getTime() ); counter=date.getTime(); System.out.println(" " + date ); */ Date tim=new Date(); counter=tim.getTime(); tim.setTime(counter+9000); new Timer().schedule(new TimerTask(){ //到指定時(shí)間就去執(zhí)行這個(gè)指定任務(wù),這里是退出操作 public void run(){ System.out.println("時(shí)間到:"); System.exit(0); } }, tim ); EventKey ke=new EventKey(); Thread1 demo1=new Thread1(ke) ; Thread2 demo2=new Thread2(ke) ; demo1.start(); demo2.start(); }
} class Thread1 extends Thread{ private EventKey ek; private int ko; public Thread1(EventKey e){ ek=e; } public void run(){ synchronized(this){ while(true){ ko=ek.next(); System.out.println(Thread.currentThread()+"ko:"+ko); if(ko % 2 !=0 ){ System.out.println("輸出的是奇數(shù)"); System.exit(0); } }}} } class Thread2 extends Thread{ private EventKey ek; private int ko; public Thread2(EventKey e){ ek=e; } public void run(){ synchronized(this){ while(true){ ko=ek.next(); System.out.println(Thread.currentThread()+"ko:"+ko); if(ko % 2 !=0 ){ System.out.println("輸出的是奇數(shù)"); System.exit(0); } }}} } class EventKey implements IntGenerator{ private int i=0; synchronized public int next(){ i++; i++; try{ TimeUnit.MILLISECONDS.sleep(1000); }catch(InterruptedException e){ System.out.println(e); } return i; } } interface IntGenerator{ public int next(); } |
現(xiàn)實(shí)當(dāng)中將對共享資源的共有操作方法放在接口或抽象類,這樣在通過繼承抽象類或?qū)崿F(xiàn)這個(gè)接口可以得到更好的效果!這樣代碼也更清晰,更有層次感!