• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Threads not running concurrent?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm trying to understand how threads work in Java. For this I use this small test program:

As output, I expect a mixed sequence of numbers, e.g.: 0 0 1 1 2 2 3 3 4 5 4 6 5 7 6 8 9 7 8 9
But what I get is: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
It looks as if the threads were running consecutively. I use netbeans to run the program.

Somebody has an explanation for this?

Thank you and best regards
polarbear
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say concurrent, that does not mean that threads are running side by side, they will take their turn to execute, and it is not that you will get the output in that sequence, it may change depending upon which thread was running and which one went in wait state. Multi threading is for situations say there are two threads, one is opening the file and another is calculating something, now in absence of multi threading, works will be sequential, so unless that file is open, no business proess will happen. But because of multi threading, in the meanwhile this thread is in waiting state for file opening, another thread will start running and do some other business.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put a Thread.sleep(100) into the for loop, and check again. Check the concurrency tutorial to understand how threading works.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The run method finishes much too quickly. The thread scheduler doesn't switch contexts *that* often.
 
Marshal
Posts: 75716
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try adding something to print the names of the Threads as well as those numbers. Look at this method; you can writein your run() method; then you can see whether the threads (probablay called thread1, thread2 or some such poetic names) are running in a particular order.

 
Campbell Ritchie
Marshal
Posts: 75716
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite the same as what you showed earlier . . .

java ThreadTest Thread1 Thread2 Thread3 Thread4

. . . and see what happens if you run that class several times.
 
Quick! Before anybody notices! Cover it up with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic