This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Main thread is terminated, After threads all threads initialized. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Main thread is terminated, After threads all threads initialized." Watch "Main thread is terminated, After threads all threads initialized." New topic
Author

Main thread is terminated, After threads all threads initialized.

Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 227
Hi All,

I'm creating 3 threads for mini Web-crawler. When 3 threads created, main thread is continuing its job and terminated. I want main thread to process some tasks after child's executed(while sleeping). What I mean is,

1) Main thread created 3 threads.(Main thread should sleep, after creating child's)
2) 3 threads are crawled some pages and gone to sleep.
3) Now, I want main thread to wake-up and do some tasks.
4) Again main thread should go to sleep and 3 child's should wake-up.

Here, I don't know how to wake-up main thread, when all child's are sleeping...

Thanks:
Ramakrishna K.C
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 838
    
  14
You could use some sort of an inter thread communication mechanism- depends on what you're comfortable with.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
It sounds like a java.util.concurrent.CountDownLatch may fit the bill.


Ping & DNS - updated with new look and Ping home screen widget
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 227
If I use java.util.concurrent.CountDownLatch, After countdown it'll(Main thread) wake-up without seeing all child threads are sleeping or not. That is the concept of java.util.concurrent.CountDownLatch Correct? Can you give some code samples?


Inter-Thread mechanism communication, Some what doing now. But, it takes a lot of code. I want simple, understandable, Less code.

Thanks:
Ramakrishna K.C
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3946
    
  17

Or if this is a cycle where the main thread gathers pages to process and the child threads process them, then you might consider using an Executor / ThreadPool. See this part of the tutorials: http://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html. Basically, you make a ThreadPool with 3 threads. The main thread submits three tasks, uses the Futures to wait for them to complete. Submits the next group of tasks, and repeat until done.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Main thread is terminated, After threads all threads initialized.
 
Similar Threads
interrupt.......
What good is a sleep when you can be interrupted before going to sleep
How to dispose an Applet
regarding daemon thread
what is your sleep pattern