This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Threads behave in sequential Style Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads behave in sequential Style" Watch "Threads behave in sequential Style" New topic
Author

Threads behave in sequential Style

sanjey asok
Ranch Hand

Joined: Jul 11, 2009
Posts: 39
I have a program which create many Thread and each one execute its process,

Among all threads ,one thread take more time(MyCallable1) to finish it process and other thread (MyCallable) take less time to finish its process,

I call the long-running Thread first and call others one by one ,but the other thread wait for the thread MyCallable1 to finish after that,It start it process.

Why other threads MyCallable wait for MyCallable1,then it is not real multithreaded?







Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

sanjey asok wrote:
Why other threads MyCallable wait for MyCallable1,then it is not real multithreaded?


How do you know this is happening? Your MyCallable don't print anything out. So, how do you know that they are actually waiting for MyCallable1 to finish?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
sanjey asok
Ranch Hand

Joined: Jul 11, 2009
Posts: 39
Henry Wong wrote:
sanjey asok wrote:
Why other threads MyCallable wait for MyCallable1,then it is not real multithreaded?


How do you know this is happening? Your MyCallable don't print anything out. So, how do you know that they are actually waiting for MyCallable1 to finish?

Henry


MyCallable return current time


After "Ending " printed ,every thread returns value
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
When I run your code, it seems to work correctly -- all threads are running concurrently.

One little aside: you don't call sleep on a specific Thread instance but rather call it as a static method,

This calls sleep on the current thread.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

sanjey asok wrote:
Henry Wong wrote:
How do you know this is happening? Your MyCallable don't print anything out. So, how do you know that they are actually waiting for MyCallable1 to finish?


MyCallable return current time

After "Ending " printed ,every thread returns value



You do know that the "current time" being returned, is for a time that was taken at the beginning of the method, right? So... all those times returned, are their start times, and not their end times. And since all the threads started around the same time, they should be close to each other.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads behave in sequential Style