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 / First Aid / September 2004

Tip: Looking for answers? Try searching our database.

Question about threads

Thread view: 
luca - 17 Sep 2004 11:13 GMT
Hi, I never really dealt with threads, but apparently
now I need to do that now.
I have a servlet which needs to retrieve a bunch
of pix in parallel trhough HTTP, so I took inspiration from this
example in the Jakarta HttpClient sample code:

http://cvs.apache.org/viewcvs.cgi/jakarta-commons/httpclient/src/examples/MultiT
hreadedExample.java?rev=1.1.2.2&view=markup


and I am now doing something:

        // start the threads
        for (int j = 0; j < threads.length; j++) {
           threads[j].start();
        }
        //my data structures are not ready :(
                //I may get NullPointerException here
        System.out.print(SomeVarThatTheThreadShouldHaveSet.toString());       

each thread then proceeds to populate some vars. The problem is
that I need to wait for all threads to complete before I move on
with my algorithm. How do I achieve that?

Also, a more general question. How expensive is it to start threads in this fashion
in a servlet environment?  is there a way to optimize? can I reuse threads accross
different servlet invocations?

Thanks

Luca
Stefan Schulz - 17 Sep 2004 12:04 GMT
> Hi, I never really dealt with threads, but apparently
> now I need to do that now.
[quoted text clipped - 18 lines]
> that I need to wait for all threads to complete before I move on
> with my algorithm. How do I achieve that?

1.5 has some nifty tools for such a situation, but you can solve it in
1.4 by using some kind of "counter" of active threads. You "main" thread
wait()s on the counter, and each thread increments it when it starts,
and decrements it once it stops. notifying the main Thread if the
counter reaches 0.
luca - 17 Sep 2004 12:23 GMT
> 1.5 has some nifty tools for such a situation, but you can solve it in
> 1.4 by using some kind of "counter" of active threads. You "main" thread
> wait()s on the counter, and each thread increments it when it starts,
> and decrements it once it stops. notifying the main Thread if the
> counter reaches 0.

Lovely. Thanks.

<shameless>
Do you happen to have a code sample too?
</shameless>

Luca
Andrew Thompson - 17 Sep 2004 12:32 GMT
>> ... You "main" thread
>> wait()s on the counter, and each thread increments it when it starts,
>> and decrements it once it stops. notifying the main Thread if the
>> counter reaches 0.
..
> <shameless>
> Do you happen to have a code sample too?
> </shameless>

if (addThread) {
 threadCount++;
} else {
 threadCount--;
}

if ( threadCount<1 ) {
notifyMainThread();
}

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane

Stefan Schulz - 17 Sep 2004 13:13 GMT
>> 1.5 has some nifty tools for such a situation, but you can solve it in
>> 1.4 by using some kind of "counter" of active threads. You "main"
[quoted text clipped - 7 lines]
> Do you happen to have a code sample too?
> </shameless>

Something like this should do the trick:

class Worker implements Runnable {
    private Object lock;

    private static int count = 0;

    public Worker(Object lock){
        this.lock = lock;       
    }

    public void run(){
        synchronized(lock){
            count++;
        }

        // your code here

        synchronized(lock){
            count--;

            if (count == 0)
                lock.notify();
        }
    }
}

Note that you still need to be sure all your threads kick of in the
beginning... so maybe you need to check if the total number of threads
has been started, or something like this. But this is the basic skeleton
of how i would implement it. Have the "main" thread wait on the lock
object (and be sure to use the same for all workers).
Thomas G. Marshall - 18 Sep 2004 05:47 GMT
Stefan Schulz coughed up:

>>> 1.5 has some nifty tools for such a situation, but you can solve it
>>> in
[quoted text clipped - 41 lines]
> skeleton of how i would implement it. Have the "main" thread wait on
> the lock object (and be sure to use the same for all workers).

This is not a request for you to do anything differently, but you should be
aware (in case you're not already) that hard tabs get stripped out
automatically by outlook express, and a huge number of people (if not most)
use it as their newsreader.

Basically, to them (and me) it looks like everything is left aligned.

Whatever.

Signature

It'salwaysbeenmygoalinlifetocreateasignaturethatendedwiththeword"blarphoogy"
.

Adam - 17 Sep 2004 13:33 GMT
> Hi, I never really dealt with threads, but apparently
> now I need to do that now.
[quoted text clipped - 16 lines]
> that I need to wait for all threads to complete before I move on
> with my algorithm. How do I achieve that?

for (int j = 0; j < threads.length; j++) {
    threads[j].join();
}

Ok, a full example:
<code>
public class WaitTest{
 public static void main(String[] args) throws InterruptedException {
   System.out.println("[main thread] Main thread started.
Constructing and starting threads...");
   Thread[] threads = new Thread[10];
    for (int j = 0; j < threads.length; j++)   {
       threads[j] = new Thread(task, "Task_"+j);
       System.out.println("[main thread] Starting task #"+j);
       threads[j].start();
    }
    System.out.println("[main thread] waiting for all the tasks to
complete...");
     for (int j = 0; j < threads.length; j++)  {
        threads[j].join();
     }
     System.out.println("[main thread] All tasks completed, can go on
with the main algorithm :)");
  }//end of main

static Runnable task = new Runnable() {
 public void run()  {
  long sleeptime = 1000+(int)(4000*Math.random());
  System.out.println(Thread.currentThread().getName()+" doing some
lenghty operation. It will take at least "+sleeptime+" ms");
  try   {
                        Thread.sleep(sleeptime);
  }catch(InterruptedException e){}//ignoring
  System.out.println(Thread.currentThread().getName()+" done!");
 }
};
}

> Also, a more general question. How expensive is it to start threads
> in this fashion
> in a servlet environment?  is there a way to optimize? can I reuse
> threads accross
> different servlet invocations?

Creating threads is expensive, no matter in what environment.
You surely can reuse the threads across servet invocation - but you
have to implement a thread pool yourself (or download such a beast),
there is no such thing in core library.

Other thing is that reusable threads usually never end their run()
method,
but wait() for their next use after finishing one task, so will never
join().
You have an idea of solution for such situation in other posts.
(main thread should wait() to stop executing; task that
completes as the last one should notify() the main thread to go on).

Adam
luca - 18 Sep 2004 19:32 GMT
> for (int j = 0; j < threads.length; j++) {
>      threads[j].join();
> }

this did the job. Thank you. I think I'll also go
for the thread pooling...

Luca
Steve Horsley - 17 Sep 2004 18:52 GMT
> Hi, I never really dealt with threads, but apparently
> now I need to do that now.
[quoted text clipped - 18 lines]
> that I need to wait for all threads to complete before I move on
> with my algorithm. How do I achieve that?

The usual approach is to use join() to wait until they've all finished:

   // start them all
   for(int j = 0 ; j < threads.length ; j++) {
       threads[j].start();
   }
   // wait for them all to finish
   for(int j = 0 ; j < threads.length ; j++) {
       threads[j].join();
   }

> Also, a more general question. How expensive is it to start threads in
> this fashion
> in a servlet environment?  is there a way to optimize? can I reuse
> threads accross
> different servlet invocations?

You can create a thing called a thread pool, but for a first go, it's
easier just to make threads when you need them. Threads are not very
expensive in java, but I have heard that "hundreds" of threads can get
bogged down in task switching.

Steve
luca - 18 Sep 2004 00:57 GMT
> Threads are not very expensive in java,

this seems to contradict the Java glossary:

http://www.mindprod.com/jgloss/thread.html

"Even trivial Java apps are designed to run on multi-cpu
machines with execution streams running in parallel.
Each stream is called a thread. Each thread, by default,
gets one megabyte of virtual RAM reserved for its stack
just in case it wants to grow. So 2000 threads would
require 2 gig of virtual RAM, -- which all by itself
would fill the largest possible 32-bit virtual
RAM space in a Wintel machine."

what's the truth?

thanks

Luca
Chris Smith - 18 Sep 2004 03:03 GMT
> > Threads are not very expensive in java,
>
[quoted text clipped - 12 lines]
>
> what's the truth?

They both are, but you may be misinterpreting Roedy's explanation.  When
Roedy says that 2000 threads would exhaust the "virtual RAM" of Windows,
he presumably means "address space", not "committed page file space".  
For the most part, a thread isn't going to use anything close to 1 MB of
stack space -- especially a short-lived single-task thread -- and that
address space won't have memory committed to it.

Of course, if you plan to approach 2000 simultaneous threads, then this
would become a problem -- but inexpensive or not, 2000 simultaneous
threads on one box would be overkill anyway.  (And you could solve the
problem by reducing the threads' stack size via the -Xss parameter to
the virtual machine).

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



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.