Results 1 to 3 of 3
  1. #1
    nikhil_me is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Exclamation synchronized run doesn't work

    Greetings Everyone!!!
    I am having some difficulty with the synchornization of thread. i tried the code

    Java Code:
    class Callme{
    	 void call(String msg){
    		System.out.print("["+msg);
    		try{
    			Thread.sleep(1000);
    		}catch(InterruptedException e){
    			System.out.println("Interrupted");
    	}	
    		
    		System.out.println("]");
    
    	}
    }
    class  Caller implements Runnable{
    	String msg;
    	Callme target;
    	Thread t;
    	
    	public Caller(Callme targ,String s){
    		target=targ;
    		msg=s;
    		t=new Thread(this);
    		t.start();
    	}
    	 public void run(){
    		
    		target.call(msg);
    		}
    
    }
    public class Sync {
    
    public static void main(String[] args) {
    	Callme target=new Callme();
    	Caller ob1=new Caller(target,"hello");
    	Caller ob2=new Caller(target,"synchronised");
    	Caller ob3=new Caller(target,"world");
    	try{
    		ob1.t.join();
    		ob2.t.join();
    		ob3.t.join();
    	}catch(InterruptedException e)
    	{
    		System.out.println("main interrupted");
    	}
    	}
    
    
    	}
    the output was
    [hello[synchronised[world]
    ]
    ]


    if i synchornize method void call(string), output changes(obvious) but when i synchronize run no change occur. shouldn't run method be blocked for other threads until thread executing it finishes????


    thnxxxxx
    Last edited by Eranga; 01-20-2012 at 09:32 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: synchronized run doesn't work

    How many time you run and see the results of your code?

    Because that's not the only output you can see if you run your code. Think again that what is mean by synchronize with relevant to your code.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: synchronized run doesn't work

    You only have one CallMe object, so synchronisation is meaningful there, but you have three different Caller objects, so why would you expect a synch on one object to have any effect on the same method on another object?

Similar Threads

  1. Why doesn't this work?
    By mailman in forum Java Applets
    Replies: 5
    Last Post: 01-10-2012, 01:01 PM
  2. Jar doesn't work
    By mad72584 in forum New To Java
    Replies: 35
    Last Post: 08-07-2011, 05:22 PM
  3. why this doesn't work?
    By hitesh_public in forum New To Java
    Replies: 5
    Last Post: 08-09-2010, 08:07 AM
  4. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 10:33 PM
  5. Why doesn't my loop work?
    By d0nmin0 in forum Advanced Java
    Replies: 8
    Last Post: 05-26-2008, 06:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •