• 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

Program required on Thread

 
Ranch Hand
Posts: 35
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Guys,

I want to write program on thread but I am not getting how to handle it. Below is the my requirement and code.




I want to excecute get.getCount() after all threads excecuted and I should get count 3. Please help me in modification.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All programs run on a thread. When you run your app several threads are created one of which is the main thread which executes the applications main method. This main thread runs your code which creates 3 more threads so now you have 4 threads of execution within your code. Because there are no guarantees on order of execution it may be that the main thread exits the main method before your other threads even run or they may all run and complete before the main thread calls getCount() or any combination in between. It's even likely that if you run the program many times you will get different results.

To get your program to work you need to pause until all threads have completed before you call the getCount() method. There are several ways of doing this but the best approach for your type of app is probably to use a java.util.concurrent.CountDownLatch
 
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
Another simple way to wait for your threads to finish is to use Thread's join() method.

Also, without synchronization, it's possible to end up with count being 1, 2, or 3 after all 3 threads have finished. Not likely, but definitely possible.

Finally, it's better to implement Runnable than to extend Thread. You're not defining a special kind of Thread. You're just defining a task to be run in a separate thread.
 
Babugouda patil
Ranch Hand
Posts: 35
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Jeff and Tony thanks for your reply.

@Jeff
I have tried to use join before this post I didn't det desired result. I want to print the count value after all threads I created so output should come 3.

How can I join main thread? Please reply

Please help me with synchronization how to handle it.
 
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

Babugouda patil wrote:
Hi Jeff and Tony thanks for your reply.

@Jeff
I have tried to use join before this post I didn't det desired result.



Then you must not have used it correctly:


output should come 3.



As I already stated, without proper synchronization, you can't guarantee that.

Please help me with synchronization how to handle it.



All access to the shared varaible must be synchronized. See a book or tutorial on multithreading for details, and post a specific question if you get stuck.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Babugouda patil wrote:
@Jeff
I have tried to use join before this post I didn't det desired result.



Well, it would actually help us if you told us what you actually tried.... and as already mentioned, your code isn't thread safe, so even with the join() call, it isn't guaranteed to work correctly.

[EDIT: Darn. Too slow ... ]

Henry
 
Babugouda patil
Ranch Hand
Posts: 35
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I used the below code it's working fine and getting expected result.


I want to know can I handle this program better than this? because I am feeling using join() method is not appropriate, what if I have 1000 of threads?
One more question is it possible to suspend main thread till all other threads excecute?
 
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

Babugouda patil wrote:Hi All,
I want to know can I handle this program better than this?



For such a small program that has no real purpose other than education, it's hard to say. There's not much else to be done. In the real world, you'd want to synchronize getCount()'s body on the Class object just like you did for run(). In this case it doesn't matter because you don't call getCount() until all the other threads are done, but in real-world code, we usually can't guarantee that that will be the case.

Also, this is bad practice:


It's not good to refer to a static member through an instance reference. Better to declare getCount() as static and just call ThreadTest1.getCount(). And even if you were going to call it through a reference variable, you could just use any one of your existing t variables--no need to create another object. (But don't do that. Just make the method static and call it using the class name.)

because I am feeling using join() method is not appropriate, what if I have 1000 of threads?



As mentioned earlier, you could also use a CountdownLatch. The code will be similar, but slightly more complex. I wouldn't bother with that here. A CDL is used to make the current thread pause until some event has been triggered N times. Waiting until N threads have died is just one particular application of that feature. The join() method, on the other hand, is specifically made to wait until a thread has died, which is exactly what you want, so, to my mind, join() is the most appropriate tool for this job.

If you had 1000 threads, the join() calls wouldn't really be your problem. The problem would be that you'd have 1000 variables. But you wouldn't do that. You'd put the threads into a Collection or array, and then to join() them all, you'd just iterate over that Collection or array. Having 1 thread or 1000 makes no difference as far as join() is concerned.

One more question is it possible to suspend main thread till all other threads excecute?



That's exactly what the join() method does. It suspends the current thread (in this case, your main thread) until the join()ed thread has completed execution.
 
Babugouda patil
Ranch Hand
Posts: 35
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ok Jeff,

Thank you very much for your reply.
 
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
You're very welcome. I hope it helped.
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic