aspose file tools*
The moose likes Beginning Java and the fly likes Question about threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about threads" Watch "Question about threads" New topic
Author

Question about threads

Joseph Tulowiecki
Greenhorn

Joined: Jun 10, 2011
Posts: 25
Just trying to learn how threads operate here. My intention is to have these two threads run at the same time but it isn't happening. Only one is running. Does anyone know why? Here is the code.



And



And



And

James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1022
    
    5

How have you confirmed that only one thread is running?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

I suggest you read Concurrency in Swing and then use javax.swing.Timer instead of threads. You're now doing stuff outside the Event Dispatcher Thread (EDT) which should be executed from the EDT.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joseph Tulowiecki
Greenhorn

Joined: Jun 10, 2011
Posts: 25
concurrency in swing, never heard of it thanks ;D ill check the out. To the post before, I guess I don't know that its not running, I didn't do a System.out.println check or anything but i dont see the circle :O
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Joseph Tulowiecki wrote:Just trying to learn how threads operate here. My intention is to have these two threads run at the same time but it isn't happening. Only one is running. Does anyone know why?


Not sure what you mean by "only one is running." There are a couple of possible interpretations of that. However, here are some basics to be aware of.

1. You can never force multiple threads to be executing at the same time. Even if you have multiple cores or multiple CPUs, there's no guarantee that the JVM and OS will schedule the threads to run at the same time. All we can do by starting multiple concurrent threads is indicate that we are allowing these various tasks to execute in parallel. The scheduling details are out of our hands.

2. If you do the following:

then t1 and t2 will be eligible to execute concurrently.

3. Despite the dire warnings in #1, if you do #2, then you will generally observe both threads running in parallel. In particular, if there is I/O involved, one thread will likely be swapped out while the it's waiting on I/O so the other can do whatever CPU-bound tasks it has, and then vice versa. If it's all CPU-bound but you have multiple cores or CPUs, then both will probably execute simultaneously, and if you have a single CPU/core, then they will take turns, and may do so fast enough that it looks like they're executing simultaneously. Note that these are generalities but in most common cases, that's the behavior you'll see.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question about threads