• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about threads

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How have you confirmed that only one thread is running?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joseph Tulowiecki
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic