Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

creatign multiple threads

 
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
?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
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.
 
(instanceof Sidekick)
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 ]
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic