Thread: Syncronization

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2

    Syncronization


    Hello, my program freaks me out. I think my understanding of programming is breaking down. Please explain me this:
    here is my code, it's very simple , as u see :
    Code:
    	class Callme{
    		 synchronized void call(String msg){
    			System.out.print("[" + msg);
    			try{
    				Thread.sleep(1000);
    			} catch (Exception ex){
    				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();
    		}
    		
    		@Override
    		public void run() {
    			target.call(msg);			
    		}		
    	}
    	
    	
    class Synch{
    		
    	public static void main(String[] args){
    		Callme target = new Callme();
    		Caller ob1 = new Caller(target, "Welcome");
    		Caller ob2 = new Caller(target, "to synchronized");
    	    Caller ob3 = new Caller(target, "world!");
    		
    		try{
    			ob1.t.join();
    			ob2.t.join();
    			ob3.t.join();
    		} catch (Exception ex){
    			System.out.println("Interupted");
    		}
    	}
    
    }
    This program was made by example of Herbert Schildt. (The same as in example.) So the program should show me the correct output. But it doesn't.
    So the correct outpu is :

    [Welcome]
    [to synchronized]
    [world!]

    My output is:
    [Welcome]
    [world!]
    [to synchronized]

    My point is: I TOTALY can't understand how the ob3-row can be compiled before ob2-row???!
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    I TOTALY can't understand how the ob3-row can be compiled before ob2-row???!
    It looks you even can't understand the difference between "compiling" and "running" the program. So I fear these matters are bit too advanced for your current level and you'd better stop and return to basics. There is nothing wrong in learning language thoroughly and step by step.

    This program creates three separate threads, which are executed independently. See, the Caller constructor creates thread and starts it. So if you create three "callers" - three threads are started.

    When three threads are executed simultaneously and try to print to console, no one could predict which message will appear first. It is like horse races - before finish you could not tell who will win, even though they were started in some specific order.
    CodeAbbey - programming problems for novice coders
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    OK, but this output is posssible in unsynchronized program. in synchronized programm 1st thread starts 1st and ends 1st, the same with others . and when programm runs, it should run step-by step. So:
    1st thread start-end
    2nd thread start-end
    3rd thread start-end
    but i have
    1
    3
    2
    or my program not synchronized or it synchronized wrong, but , i think , there is no other way .
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    Why do you think your program is synchronized? There is nothing about synchronization in this code except that method "call" is marked as "synchronized" which is completely useless since it do not access any shared variables.

    The only influence of this "synchronized" is that method "call" could not be entered by all three threads at once. However, it does not determine with which "call" would be called first.

    See, your code is executed as following:
    - three constructors are called, three threads are created and their "start" methods are called - perhaps it is done in a single slice of processor time;
    - then we enter section where "join" is called and the main thread is paused;
    - now thread-manager has three thread in "started" state (but not in progress) and one thread in paused state;
    - thread-manager starts execution of the three threads - if you have 4 processor cores, their "run" methods could be started at once, otherwise two of them are started and one is still waiting;
    - there is no any rule about which of the threads will capture the "monitor" of the "target" object first; for example it could be first of child thread;
    - after that two other child threads are be blocked;
    - you may think that they receive initiative in turns, and peek whether "target" is freed or not;
    - sooner or later "target" is unlocked and one of two threads captures its "monitor" again; you could not tell who of them it would be - they were both waiting for it, and none have precedence.
    CodeAbbey - programming problems for novice coders
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    I understood this. Thank you.
    Last edited by FAQer; November 25th, 2013 at 12:52 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    I understood this . Thank you.

IMN logo majestic logo threadwatch logo seochat tools logo