• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

run two thread at the same time in java

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what does the code do now, and how would you like it to behave instead?
 
nikki sinha
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
nikki sinha
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 48
Eclipse IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic