Synchronization with threads

J

javateamPL

Hello,

I have a class which is not a thread which executes the following
code:
public void myFunc() {
MyMainThread thread = new MyMainThread();
thread.start();
....
exec(arrayListFromThread);
....
}

A thread creates many subthreads which role is to fill in the
ArrayList (defined for thread). Next, I would like to pass this
arrayList to myFunc in the moment when all subthreads have finished
its job (so that I am sure that arraylist is complete) and immediately
start exec method.

I have two problems:
- how to pass this arrayList from thread (can I simply use method from
thread to return it? will the thread be still active?)
- how can I suspend an execution of myFunc() till the mainThread
finish creating arrayList?

Would be very grateful for effective solutions of these problems.

Regards, Mark
 
J

Janusch

Hello,

I have a class which is not a thread

======================================
1. If you use a main method, you use the main thread.
In this case it is ok to wait for your MyMainThread.

public void myFunc() {
MyMainThread thread = new MyMainThread();
thread.start();
....
while (thread.isAlive()) try{ Thraed.sleep(100); } catch(Exception err)
{}
exec(thread.getArrayListFromThread());
....

}

class MyMainThread extends Thread{
private ArayList arrayListFromThread;

public void run(){
//start all Subthreads
while ( /*one of the subtreads runs */) {
try{ Thraed.sleep(100); } catch(Exception err){}
}
}

public ArrayList getArrayListFromThread(){return arrayListFromThread;}
}
======================================
2. If you use a Swing UI, you use the Swing-Thread.
In this case is it better to invoke exec(arrayListFromThread) in
MyMainThread.

public void myFunc() {
MyMainThread thread = new MyMainThread();
thread.start();
}

class MyMainThread extends Thread{
private ArayList arrayListFromThread = new ArrayList();

public void run(){
//start all Subthreads
while ( /*one of the subthreads runs */) {
try{ Thraed.sleep(100); } catch(Exception err){}
}
exec(arrayListFromThread);
....
}
}

======================================
All Subthreads have to synchronize all access to ArayList!

I have two solution for starting all Subthreads and waiting:
solution 1:

Thread[] t=new Thread[200];

public void run(){
for (int i...){
t=new Thread();
t.start();
}
while ( oneSubThreadIsAlive()) {
ry{ Thraed.sleep(100); } catch(Exception err){}
}
....
}

public boolean oneSubThreadIsAlive(){
for (int i...) if (t.isAlive()) retrun true;
return false;
}


solution 2:

Thread[] t=new Thread[200];
int runs=0;
Object mon=new Object();

public void run(){
for (int i...){
t=new SubThread();
t.start();
synchronized(mon){runs++;}
}
while ( runs>0) {
try{ Thraed.sleep(100); } catch(Exception err){}
}
....
}

class SubThread extends Thread{
public void run(){
try{
...
// fill ArayList used synchronized-block
...
// ready:
}finally{ synchronized(mon){runs--;} }
}
}

================================
Filling a ArayList used synchronized-block:
synchronized(mon){arrayListFromThread.add("something");}

Janush
 
D

Daniel Pitts

Hello,

I have a class which is not a thread which executes the following
code:
public void myFunc() {
MyMainThread thread = new MyMainThread();
thread.start();
....
exec(arrayListFromThread);
....
}

A thread creates many subthreads which role is to fill in the
ArrayList (defined for thread). Next, I would like to pass this
arrayList to myFunc in the moment when all subthreads have finished
its job (so that I am sure that arraylist is complete) and immediately
start exec method.

I have two problems:
- how to pass this arrayList from thread (can I simply use method from
thread to return it? will the thread be still active?)
- how can I suspend an execution of myFunc() till the mainThread
finish creating arrayList?

Would be very grateful for effective solutions of these problems.

Regards, Mark
this sounds like a bad idea, unless you make sure your using proper
synchronization...

I suggest looking into the standard ExecutorService and
CompletionService classes.
 
K

Kevin McMurtrie

Daniel Pitts said:
this sounds like a bad idea, unless you make sure your using proper
synchronization...

I suggest looking into the standard ExecutorService and
CompletionService classes.

There's nothing wrong with that. The ArrayList can be accessed if it's
final and the Thread/Runnable is an anonymous inner class. It can also
be accessed if the array is a field of the class. It can be passed in
the constructor for a Thread subclass. Many options.

Each thread will have to synchronize on a common object, probably the
ArrayList itself, while working on it. Call join() on each Thread to
block until it completes.
 
D

Daniel Pitts

Kevin said:
There's nothing wrong with that. The ArrayList can be accessed if it's
final and the Thread/Runnable is an anonymous inner class. It can also
be accessed if the array is a field of the class. It can be passed in
the constructor for a Thread subclass. Many options.
I didn't say the array wasn't accessible.
Each thread will have to synchronize on a common object, probably the
ArrayList itself, while working on it. Call join() on each Thread to
block until it completes.

That would be the way to do it with his current approach, but I was
suggesting he look into a much cleaner, more efficient, and
easier-to-get-correct solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top