aspose file tools*
The moose likes Threads and Synchronization and the fly likes run two thread at the same time in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "run two thread at the same time in java" Watch "run two thread at the same time in java" New topic
Author

run two thread at the same time in java

nikki sinha
Greenhorn

Joined: Jul 04, 2012
Posts: 14
hi,

i have used timertask to schedule my java program. now when the run method of timertask is in process, i want to run two threads which run at the same time and do different functions. here is my code.. please help me..

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41886
    
  63
So what does the code do now, and how would you like it to behave instead?


Ping & DNS - my free Android networking tools app
nikki sinha
Greenhorn

Joined: Jul 04, 2012
Posts: 14
Ulf Dittmer wrote:So what does the code do now, and how would you like it to behave instead?


the code runs the t1 thread first and then the t2 thread. i want to start them at the same time.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41886
    
  63
The threads run for very short times. It may not be worthwhile in that short time to switch between them. Try running the loop to 10000000 instead of 10.
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

nikki sinha wrote:the code runs the t1 thread first and then the t2 thread. i want to start them at the same time.

You can have a look at java.util.concurrent.CountDownLatch to make threads start at the same time

Oh, one more thing. You are trying to run same thread instance more than once. That wont work; meaning the code will run the threads only once in their lifetime and the next scheduled run of timercheck will fail.


Warm Regards,
Rakesh
nikki sinha
Greenhorn

Joined: Jul 04, 2012
Posts: 14
Ulf Dittmer wrote:The threads run for very short times. It may not be worthwhile in that short time to switch between them. Try running the loop to 10000000 instead of 10.


i tried it for more lines also. but it executes the first thread, stops in between, gives chance to second thread, then again runs the first thread and so on... i want to run them parallely.
nikki sinha
Greenhorn

Joined: Jul 04, 2012
Posts: 14
Rakesh K. Cherukuri wrote:
nikki sinha wrote:the code runs the t1 thread first and then the t2 thread. i want to start them at the same time.

You can have a look at java.util.concurrent.CountDownLatch to make threads start at the same time

Oh, one more thing. You are trying to run same thread instance more than once. That wont work; meaning the code will run the threads only once in their lifetime and the next scheduled run of timercheck will fail.


thank you, i tried to use countdownlatch also, but may be i am doing something wrong. my code runs the first thread, stops in between, start the second thread, and then again start the first thread. they are not runnning parallely. here is my modified code.

Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

Looking at the code, they do run in parallel. If you are looking at the program output and dont see the numbers interspersed then thats because it runs so fast like Dittmer mentioned.

Here is what you can do. Change the for loop code (loop count 10 should be enough)in each thread to following and then execute


The above code will let the threads share the println()

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

There's no way to force multiple threads to run in parallel. By putting tasks in separate threads, you're only saying that your program logic allows them to run simultaneously. It's up to the JVM and OS to decide whether to run one after the other, have them take turns, or run them simultaneously on separate cores.
nikki sinha
Greenhorn

Joined: Jul 04, 2012
Posts: 14
Rakesh K. Cherukuri wrote:Looking at the code, they do run in parallel. If you are looking at the program output and dont see the numbers interspersed then thats because it runs so fast like Dittmer mentioned.

Here is what you can do. Change the for loop code (loop count 10 should be enough)in each thread to following and then execute


The above code will let the threads share the println()



Yhank you so much. now i am more clear about my output.
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

Jeff Verdegan wrote:There's no way to force multiple threads to run in parallel. By putting tasks in separate threads, you're only saying that your program logic allows them to run simultaneously. It's up to the JVM and OS to decide whether to run one after the other, have them take turns, or run them simultaneously on separate cores.
Hmmm, partially true i believe.

Case 1: if i am looking at those two threads from JVM perspective, each thread is not waiting for other thread to complete. That said they are running in parallel.

Case 2: if i am looking at those two threads from OS level, either each thread gets its opportunity in a time sliced manner or actually run parallel depending on the processing power the machine possess.

FWIW, OP question belongs to Case 1.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rakesh K. Cherukuri wrote:
Jeff Verdegan wrote:There's no way to force multiple threads to run in parallel. By putting tasks in separate threads, you're only saying that your program logic allows them to run simultaneously. It's up to the JVM and OS to decide whether to run one after the other, have them take turns, or run them simultaneously on separate cores.
Hmmm, partially true i believe.

Case 1: if i am looking at those two threads from JVM perspective, each thread is not waiting for other thread to complete. That said they are running in parallel.

Case 2: if i am looking at those two threads from OS level, either each thread gets its opportunity in a time sliced manner or actually run parallel depending on the processing power the machine possess.

FWIW, OP question belongs to Case 1.


That's the difference between concurrent execution and simulataneous execution. By running two things in multiple threads, they do execute concurrently, but not necessarily simultaneously.

I was under the impression that the OP wanted to force simultaneous execution, but apparently not.
 
 
subject: run two thread at the same time in java