Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

creatign multiple threads  RSS feed

 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I want to develop an application which will spawn multiple threads.
for example

An applet which has a thread which scrolls text from lef to right
another thread which scrolls text from right to left at the same time.

using only one class is this possible?

i have been trying to do this but doesnt the class have only one run method then how will we make it do different things.
My guess is you need multiple classes to do multiple threads.
Whats your suggestion
?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you must have multiple threads, you can do it by setting a flag telling the instance if it should be scrolling left or right. Then you can do it in one class.

I don't understand why it should be multithreaded though. You shouldn't be painting in a seperate thread anyway, so why not just override the paint() of your component? (You will end up doing this anyway)
 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi timmy
actually that was not my issue.
in general i want to know how you can spawn more than 2 threads
this may be used for a game.
This question came across my mind when i was planning to design a chat s/w
but dont take that as an example.
if you could just tell me or direct me on how to use more than 2 threads(3,4,5,....) threads then it would be wonderful.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can just start as many as you like. Put the code that will do the work in a class that implements Runnable. If you don't have to keep track of the threads, just fire and forget:

To start a bunch of threads and wait for them all to finish, look into join()

There are many other tricks for communicating between your main thread and the work threads. Read up on the Thread class and the neat utilities in the JDK 5 concurrent package. Here's a Sun Tutorial for more fun. See the SwingUtilities class if your threads need to update the GUI.
[ May 20, 2005: Message edited by: Stan James ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!