File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Starting Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Starting Threads" Watch "Starting Threads" New topic

Starting Threads

Marcus Senna

Joined: Feb 22, 2007
Posts: 21
How could I start running multiple threads at the same time?
M Easter
Ranch Hand

Joined: Feb 11, 2007
Posts: 133
Bruce Eckel's book "Thinking in Java" contains an intro to threads. It is available online for free (easily googled)


M Easter
Software Composer -
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The Sun Conurrency Tutorial is pretty good, too.

In short

0) Read the tutorial or Thinking in Java.

1) Write a class that implements the Runnable interface. Put the guts of the work to be done in the run() method, pass any data required to do the job to the constructor.

2) Do something like this:

When it comes time to get data back from the thread or make two threads interact with each other, things get trickier. The Sun tutorial will get you there. BTW: This all got much easier in Java5. If you're in an older JDK, see if you can get up to Java5.

Let us know how you make out!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Marcus Senna

Joined: Feb 22, 2007
Posts: 21
Hey, What I am doing is creating an application which allows users to simulate certain communications protocols. This application would allow users to configure networks using network components such as routers and hosts by dragging and dropping images of them onto a configuration area. and then setting up links between them. I have created a thread to deal with Host objects sending data, another thread to deal with propagation delay in Link objects and another thread to deal with routing in the Router objects. I have a timer thread which allows me to vary the rate at which Host objects send data and also to allow me to simulate a propagation delay in the Link objects. The problem i am getting is that, after the Host objects have sent all the data and I return from that thread, it seems as though the other two threads are stopping also. I would like the other two threads to continue running until all the data is sent to the destination Host object. Thanks in advance for your feedback!!
I agree. Here's the link:
subject: Starting Threads
It's not a secret anymore!