Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2008

Tip: Looking for answers? Try searching our database.

Synchronization with threads

Thread view: 
javateamPL@gmail.com - 08 Feb 2008 11:43 GMT
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
Janusch - 08 Feb 2008 12:52 GMT
On 8 Feb., 12:43, javatea...@gmail.com wrote:
> 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[i]=new Thread();
  t[i].start();
}
while ( oneSubThreadIsAlive()) {
   ry{ Thraed.sleep(100); } catch(Exception err){}
}
...
}

public boolean oneSubThreadIsAlive(){
 for (int i...) if (t[i].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[i]=new SubThread();
  t[i].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
Daniel Pitts - 08 Feb 2008 15:23 GMT
> Hello,
>
[quoted text clipped - 23 lines]
>
> 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.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Kevin McMurtrie - 08 Feb 2008 15:43 GMT
> > Hello,
> >
[quoted text clipped - 28 lines]
> 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.

Signature

I don't read Google's spam.  Reply with another service.

Daniel Pitts - 08 Feb 2008 15:52 GMT
>>> Hello,
>>>
[quoted text clipped - 33 lines]
> 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.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.