my dog learned polymorphism
The moose likes Java in General and the fly likes concurrent threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "concurrent threads" Watch "concurrent threads" New topic

concurrent threads

Maurice Lowenthal

Joined: Mar 14, 2001
Posts: 4
How can I run multiple threads that mimic the usage of multiple concurrent users? I would like all my threads to start at the same time. They will all be doing the same thing
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128

public class Q implements Runnable {
public static void main(String[] args) {
Q q = new Q();
for (int i = 0; i < 4; i++)
new Thread(q).start();
public void run() {
for (int i = 0; i < 10; i++)
This code starts 3 threads that executes the run() method of your target object. Have fun!
Ashish Jaiman
Ranch Hand

Joined: May 01, 2001
Posts: 47
U can simulate an experience of multi processing or having multiple threads running simultaneously but be sure it can only happen on a multiprocessor environment all u can have is having same number of threads running AT THE SAME TIME as the number of processors.
But the real question is "simulating" multiprocessing environment having many threads running in ur program and though it might feel like they are running AT THE SAME TIME but only one and only one thread is running at a given moment of time.
SO to simulate the AT THE SAME TIME experience u can either implement runnable or extent thread class and call the start method of the thread, but remember there would be no guarantee that all ur threads would run and get the same processing time and be sure to put some yielding mechanism (sleep, wait, yield) for other threads.

-AJ<br />Sun Certified Java Programmer<br />Microsoft Certified Software Developer
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
You can have multiple threads running, and you can have multiple users working on the same object. But it's true that, behind the curtains, in a single-processor environment, only ONE thread executes at any particular time.
And Ashish is right. To really give everyone of your threads a chance, you should make the threads stop executing once in a while. To let the weaker or low-priority threads have their turn as well!
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Kaspar, I believe your code creates four threads, not three.

"I'm not back." - Bill Harding, Twister
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Hrrmmm... Well, I always was a lousy calculator... That's why I bought my computer in the first place...
Rita Williams

Joined: Jan 30, 2001
Posts: 10
If all your users are doing the same thing, like changing the contents of a database for example, you may need to write synchronized methods.
I agree. Here's the link:
subject: concurrent threads
It's not a secret anymore!