Forums Register Login

Question about threads

+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
How have you confirmed that only one thread is running?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.

Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1104 times.
Similar Threads
Creating Arrow Icon Help
painting custom components without extending jcomponent
Calling an applet from another class of the same package
Parallel dragging
Draw a curve in Java if only have 3 coordinates known.....Help !!
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:30:52.