Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2010
    Posts
    19
    Rep Power
    0

    Default Why my threads don't run simultaneously?

    Hi everyone.

    I just wrote a small example about how to create threads but they just can't work as I expected.
    This program has two threads. Each thread do the same thing: they show a name and a number. What I was expecting is these two threads run at the same time, I mean, I could see some outputs from the first thread and suddenly some outputs from the second thread, after that, more outputs from the first one again... but what is happening is the first thread run from the beggining until the end and just after it is finished, the second thread starts working.

    Could someone tell me what I did wrong?
    Thanks a lot!

    Java Code:
    public class TestarThreads {
    	
    	public static void main(String[] args) {
    		
    		TMyThread p1 = new TMyThread("A");
    		TMyThread p2 = new TMyThread("B");
    		
    		Thread t1 = new Thread(p1);
    		Thread t2 = new Thread(p2);
    		
    		t1.start();
    		t2.start();
    	}
    }
    
    class TMyThread implements Runnable {
    
    	private String name;
    	
    	TMyThread(String name) {
    		this.name = name;
    	}
    	
    	public void showNumbers() {
    		for (int i=1; i<=50; i++) {
    			System.out.println("Thread " + this.name + ": " + i);
    		}
    	}
    	
    	public void run() {
    		showNumbers();
    	}
    
    }
    The output in my computer is:
    Thread A: 1
    Thread A: 2
    Thread A: 3
    ... // here there is not any output from thread B
    Thread A: 50
    Thread B: 1
    Thread B: 2
    ...
    Thread B: 50

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Throw in a sleep() to slow your threads down a bit.

  3. #3
    JavaJuJitZu is offline Member
    Join Date
    Jan 2010
    Posts
    26
    Rep Power
    0

    Default

    Hi Gilvan,

    looking at your code it doesn't look as though you did anything wrong.
    As pBrockway2 suggested try a small sleep() or even a Thread.yield() in your loop.

    The behaviour is most likely due to thread 2 being starved of cpu time. So thread 1 starts, hogs all the cpu preventing any other process or thread from executing its tasks.

    Google thread starvation.

    The behaviour would be expected if you had done the following
    Java Code:
    t1.start();
    t1.join();
    t2.start();
    t2.join();

  4. #4
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    You can also change the priority of your threads.
    Please don't laugh at my English... I'm trying my best! :)

  5. #5
    Join Date
    Jan 2010
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Throw in a sleep() to slow your threads down a bit.
    Hi pbrockway2, thank you for your reply.

    In fact, the threads were spending few CPU time. I added a sleep of 200 milisseconds and the output was how I expected. Great!
    After that, I removed the sleep instruction and changed the for looping to run 1000 times instead of 50. After the 652th output from the first thread, I could see the first output from the second one.

    Gilvan Justino

  6. #6
    Join Date
    Jan 2010
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by JavaJuJitZu View Post
    The behaviour would be expected if you had done the following
    Java Code:
    t1.start();
    t1.join();
    t2.start();
    t2.join();
    Hi JavaJuJitZu,

    After I changed the code to loop 1000 times, I tested with the instruction join() and the first thread started and finished completely and just after that, the second thread started outputing, just as you said. Thank you for teaching me that!

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    In fact, the threads were spending few CPU time.

    Yes, exactly. I'm glad you've got that sorted out.

  8. #8
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Here's some background info on why this is happening:
    println calls are VERY fast. So, in even the limited amount of CPU time that a single thread gets,the println call is made, in your case, 652 times. It also probably has something to do with your OS - on windows, threads similar to yours have resulted in this...
    [code]
    A mesA msage fressageom ThThreadre#1
    ad#2

    [code]
    which ideally would have been
    [code]
    A message from Thread#1
    A message from Thread#2
    [code]
    Looks weird, no?
    This is because println isn't synchronized...

    So, all in all... synchronize println calls, and don't use too many in a loop...

    Cheers,
    Brandon

    BTW, most thread examples use sleep because it is such a common method for waiting...
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. GUI and Threads
    By rp181 in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-10-2009, 09:39 PM
  2. Replies: 1
    Last Post: 12-21-2008, 10:06 AM
  3. Threads!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-04-2008, 05:11 PM
  4. Using threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 11:25 AM
  5. Threads
    By one198 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-20-2007, 07:15 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
  •