Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Need help for understanding the Program

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help for understanding the Program

    I learning Java using Complete reference in book. I am in Multithreded Programming Concept know. Kindly help me to understand the logic behind this program..

    // This program is not synchronized.
    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);
      }
    }
     
    class Synch {
      public static void main(String args[]) {
        Callme target = new Callme();
        Caller ob1 = new Caller(target, "Hello");
        Caller ob2 = new Caller(target, "Synchronized");
       Caller ob3 = new Caller(target, "World");
     
        // wait for threads to end
        try {
          ob1.t.join();
          ob2.t.join();
          ob3.t.join();
        } catch(InterruptedException e) {
          System.out.println("Interrupted");
        }
      }
    }

    Output of the above Program is

    Hello [Synchronyzed[world]
    [
    [
    Explain in step by step manner if possible.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,534
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default Re: Need help for understanding the Program

    What do you think happens? How do you think this code works?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Need help for understanding the Program

    Quote Originally Posted by padhu1989 View Post
    Hello [Synchronyzed[world]
    [
    [
    Do you think this is the only possible output?
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  4. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help for understanding the Program

    Quote Originally Posted by andbin View Post
    Do you think this is the only possible output?
    Yes. This is what I can see as output in that book... I cant understand the execution steps of this program..

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Need help for understanding the Program

    Quote Originally Posted by padhu1989 View Post
    Yes. This is what I can see as output in that book...
    No, that is not the only possible output!
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,534
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default Re: Need help for understanding the Program

    Quote Originally Posted by padhu1989 View Post
    Yes. This is what I can see as output in that book... I cant understand the execution steps of this program..
    That output is just one example of what could happen. Have you run this code yourself?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Need help for understanding the Program

    Quote Originally Posted by padhu1989 View Post
    I cant understand the execution steps of this program..
    You must know that when threads are involved, there is very little that is "guaranteed".
    For example:

    thread1.start();
    thread2.start();
    thread3.start();

    There's absolutely no guarantee that the run() of thread1 really starts before the run() of thread2 (and run() of thread2 before that of thread3).
    And even if "casually" the order would be this, the thread scheduler is completely free to suspend the thread at any given moment and give the CPU to another thread.

    Thus, basing on this explanation, are you still sure that the print of:
    [Hello (by Caller ob1)
    [Synchronized (by Caller ob2)
    [World (by Caller ob3)

    will always happen in this exact order?
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

Similar Threads

  1. Need help with understanding arrays
    By mist4lyf in forum Collections and Generics
    Replies: 2
    Last Post: April 7th, 2013, 10:38 AM
  2. Need help understanding Java
    By Jafke104 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 30th, 2013, 11:52 PM
  3. Helping Understanding???
    By SuperDad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2012, 06:53 PM
  4. Need Help Understanding Where I went wrong
    By basedoverlord12 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 1st, 2011, 06:40 PM
  5. Help me Understanding Please...
    By Jabetha in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 17th, 2011, 02:55 PM

Tags for this Thread