Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Starting Threads

 
Marcus Senna
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How could I start running multiple threads at the same time?
 
M Easter
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruce Eckel's book "Thinking in Java" contains an intro to threads. It is available online for free (easily googled)

Mike
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Marcus Senna
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic