aspose file tools*
The moose likes Threads and Synchronization and the fly likes Program required on Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Program required on Thread" Watch "Program required on Thread" New topic
Author

Program required on Thread

Babugouda patil
Ranch Hand

Joined: Mar 13, 2011
Posts: 35


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.


SCJP 1.6 91%
Take certification exam as a point of learning not just for certificate
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2315
    
  49
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
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 13, 2011
Posts: 35


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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


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

Joined: Mar 13, 2011
Posts: 35

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 13, 2011
Posts: 35


Ok Jeff,

Thank you very much for your reply.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're very welcome. I hope it helped.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Program required on Thread