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 5 of 5

Thread: problem in Threading !!!

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem in Threading !!!

    class A implements Runnable
    {
    public void run()
    {
    System.out.println(Thread.currentThread());
    //Thread.yield();
    System.out.println(Thread.currentThread());
    }
    }
    class Test
    {
    public static void main(String args[])
    {
    A a1=new A();
    A a2=new A();
    A a3=new A();
    Thread t1=new Thread(a1,"SINGO");
    Thread t2=new Thread(a2,"BINGO");
    t1.setPriority(2);t2.setPriority(7);
    t1.start();t2.start();
    }
    }

    why there is no difference in output after commenting the Thread.yield( )
    with Thread.yield( ) & Without Thread.yield( ) output is
    Thread[SINGO,2,main]
    Thread[BINGO,7,main]
    Thread[SINGO,2,main]
    Thread[BINGO,7,main]
    Last edited by helloworld922; July 8th, 2010 at 01:57 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: problem in Threading !!!

    Please surround your code with [highlight=Java] and [/highlight], it makes it much easier to read.

    Here's what I think is happening:

    Thread SINGO runs, gets to yield and lets BINGO run. BINGO gets to yield, and lets SINGO run. SINGO finishes, then BINGO finishes.

    This results in the exact same output without the yield:

    SINGO starts first, prints out thread info. BINGO starts, prints out thread info. SINGO prints out thread info and finishes. BINGO prints out thread info and finishes.

    edit:

    Here's a simple test class to get around this issue:

    public class Test
    {
    	public static class A implements Runnable
    	{
    		@Override
    		public void run()
    		{
    			System.out.println("Thread A");
    			Thread.yield();
    			System.out.println("Thread A");
    		}
    	}
     
    	public static class B implements Runnable
    	{
    		@Override
    		public void run()
    		{
    			System.out.println("Thread B");
    			System.out.println("Thread B");
    		}
    	}
     
    	public static void main(String[] args)
    	{
    		A a = new A();
    		B b = new B();
    		Thread t1 = new Thread(a);
    		Thread t2 = new Thread(b);
    		t1.start();
    		t2.start();
    	}
    }

    There are some strange quirks with how the threads are executed, but with yield in there I get one of three possibilities: ABBA ("expected" result), AABB (thread A finishing before thread B even starts) or BBAA (strange quirk where thread B starts before thread A does? shouldn't ever happen...).

    Without yield, I always get AABB
    Last edited by helloworld922; July 8th, 2010 at 02:13 PM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem in Threading !!!

    If running this on a computer with more than a single processor (seems most these days have at least 2 processors), the multi-threading issue could get complicated. I don't know how the JVM divides up the work between multiple processors, or if yield works internally to the JVM or externally to the system, but the results above don't surprise me.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problem in Threading !!!

    See Postings on other forums for more discussions on this.

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: problem in Threading !!!

    You can never guarantee the order of how the threads will be run, therefore you will see the result BBAA. Even though Thread A is started before Thread B it doesn't mean the thread scheduler will run that thread first.

    Never expect concurrent threads to run in a specific order.

    // Json

Similar Threads

  1. Replies: 1
    Last Post: March 23rd, 2010, 02:29 AM
  2. Threading question....
    By neo_2010 in forum Threads
    Replies: 1
    Last Post: September 2nd, 2009, 02:38 AM
  3. Problem in Threading , ThreadGroup
    By neo_2010 in forum Threads
    Replies: 1
    Last Post: August 30th, 2009, 11:19 PM