public class SynThread{ public static void main(String args[]){ Share s=new Share(); MyThread m1=new MyThread(s,"Thread1"); MyThread m2=new MyThread(s,"Thread2"); MyThread m3=new MyThread(s,"Thread3"); } } class MyThread extends Thread{ Share s; MyThread(Share s,String str){ super(str); this.s=s; start(); } public void run(){ s.doword(Thread.currentThread().getName()); } } class Share{ public synchronized void doword(String str){ for(int i=0;i<5;i++){ System.out.println("Started :"+str); try{ Thread.sleep(100); }catch(Exception e){} } } } |
Output of the program is:
C:\j2se6\thread>javac SynThread.java C:\j2se6\thread>java SynThread Started :Thread1 Started :Thread1 Started :Thread1 Started :Thread1 Started :Thread1 Started :Thread3 Started :Thread3 Started :Thread3 Started :Thread3 Started :Thread3 Started :Thread2 Started :Thread2 Started :Thread2 Started :Thread2 Started :Thread2 C:\j2se6\thread> |
Comments
Post a Comment