Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

Multithreading in Java

Hey,

       I'm fairly new to threads in Java... and I was wondering if it was possible to start threads from threads...

Basically... is it possible to say...

thread_1(){
   if (some condition is met)
      start n threads
      thread_1.sleep(till n threads finish)
}

I know that a thread in Java can be just a simple call saying thread.start(), but I didn't find anything that shows me how to start a variable number of threads, and get the parent thread to wait for its child threads to finish.

Is there a "simple" way to do this?

Thanks in advance!
0
errang
Asked:
errang
  • 6
  • 5
  • 3
2 Solutions
 
for_yanCommented:
0
 
for_yanCommented:
0
 
errangAuthor Commented:
Hm... but how do you end up starting multiple threads?  Also, would it be possible for all the threads to share a variable?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
for_yanCommented:
Yes it is possible to share variables

I don't understand what you eman by ending up starting - you statrt those which you need and then start no more threads, maybe I'm missing...
0
 
errangAuthor Commented:
Sorry, I want to write a parallelized algorithm to explore a graph.

So, basically, I want to fork when I get to a node with multiple edges, and I wanted to fork n times for n edges from the node.

So, I need to keep track of what nodes I visited, and also have the threads update a single variable so I don't have duplicate data.
0
 
for_yanCommented:
I think if you are within a single class and you have instance variable this variable
will be shared within all threads. I think you can also have something like a collection
which could be shared between threads.
I think there are no formal liimtations to waht you want to do, how this would work when  you have many many
threads -  that I don't know - I didn't have such tasks myself, perhpas someone
else will help. I think there was some similar question here on EE about exploring graphs with threads. Maybe it makes sense to
search.
0
 
errangAuthor Commented:
I did search for a few hours, most of them were high level implementations, and i couldn't understand the questions in some cases, lol.  Guess I'll try out what you said for now.
0
 
for_yanCommented:
Sure, give it a try. I guess that these things may be specific to implementation.
I would be cautiust if you want to have really dozens  of threads, though.
But maybe I'm cautious in general :)
0
 
objectsCommented:
you can start as many threads as you want, and you can start them from anywhere in your code

to wait for a thread to finish you use the join() method

threadToWaitFor.join();

this method will block the current thread untill the thread is finished.
http://download.oracle.com/javase/tutorial/essential/concurrency/join.html

to wait for multile threads you can use a join in a loop()

for example


// strat 10 threads

Thread[] mythreads = new Thread[10];
for (int i=0; i<10; i++) {
    mythreads[i] = new Thread(new MyRunnable());
   mythreads[i].start();
}

// now wait for them to finish


for (int i=0; i<10; i++) {
   mythreads[i].join();
}

Open in new window

0
 
errangAuthor Commented:
In your code...

// strat 10 threads

Thread[] mythreads = new Thread[10];
for (int i=0; i<10; i++) {
    mythreads[i] = new Thread(new MyRunnable());
   mythreads[i].start();
}

// now wait for them to finish


for (int i=0; i<10; i++) {
   mythreads[i].join();
}

Open in new window


for (int i=0; i<10; i++) {
   mythreads[i].join();
}

Open in new window


would join all the threads together right? meaning all the threads are waiting for each other to finish?

hm... I'm just confused, how would I go about saying mythreads[0] is supposed to wait for mythreads[1], mythreads[2], mythreads[3]? Since the join() function doesn't take parameters.
0
 
objectsCommented:
> would join all the threads together right? meaning all the threads are waiting for each other to finish?

yes (sort of). the current thread will wait for the other 10 (it created to finish)

> hm... I'm just confused, how would I go about saying mythreads[0] is supposed to wait for mythreads[1], mythreads[2], mythreads[3]? Since the join() function doesn't take parameters.

using a loop as I posted above. It will wait for each of them, one at a time
0
 
errangAuthor Commented:
>>using a loop as I posted above. It will wait for each of them, one at a time

So, basically I write:

for(int i = 1; i < 4; i++){
    mythreads[i].join();
}

Open in new window


Is that it? to join mythreads[1], mythreads[2], mythreads[3] to mythreads[0]?

Also... I just noticed that we have a set number of threads here right? the size of the array? Is it possible to have an "anonymous" thread object? and then just let it run?

Like the Factory design method, or do I just have to create an array of threads whenever I need to use multiple threads?
0
 
objectsCommented:
yes thats all you need

> Is it possible to have an "anonymous" thread object? and then just let it run?

yes it is

> Like the Factory design method, or do I just have to create an array of threads whenever I need to use multiple threads?

sure, they are just objects you can use them however you like
0
 
errangAuthor Commented:
Sweet, thanks a lot!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now