Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-09-2010, 10:57 AM   #1
LQ Newbie
Registered: Jan 2003
Location: Sweden
Distribution: Ubuntu Server
Posts: 27

Rep: Reputation: 15
Question Java threads, why does this work/not work?


I'm writing a program to learn about threads in Java, consisting of one Main class and two different thread implementations (T1 and T2).

T2 is the one I have problems with. I have included a method in the class to allow me to pause the thread, and another method to stop the thread from running entirely. I've accomplished this by nesting a loop inside a loop in the run-method, and two booleans set by those methods I mentioned.

The problems is that if I pause the thread and then try to resume it it just won't do the things in the inner loop (as it should while not paused). But, and here comes the mystery, it works if I add a dummy line system.out.print(""); inside the outer loop. Further on, if I don't add the dummy line the thread won't terminate when told to and I can only exit the program by forcing it to.

Any ideas as to why it works, or doesn't work?

Platform: Mac OS X Snow Leopard, javac 1.6.0_20, java JRE 1.6.0_20-b02-279-10M3065

Here comes what I think is the relevant code...

Excerpt from - main-method
T2 t2 = new T2(); //Create and start thread T2

Thread.currentThread().sleep(5000); //Wait...

t2.toggleRunning(false); //Pause the thread T2

Thread.currentThread().sleep(5000); //Wait...

t2.toggleRunning(true); //Resume running T2

Thread.currentThread().sleep(5000); //Wait...

t2.kill(); //Stop running T2
Excerpt from
public class T2 implements Runnable {

 private Thread t = new Thread(this);
 private boolean alive;
 private boolean running;

 //Constructor: t.start(); alive = true; running = true;

 public void kill() {
  this.running = false;
  this.alive = false;

 public void toggleRunning(boolean running) {
  this.running = running;

 public void run() {
  while(this.alive) {
   System.out.print(""); // <<<<<****** DUMMY LINE
   if(this.running) {
    System.out.println("T2 running");
    try { Thread.currentThread().sleep(1000); } catch(Exception e) {}

Any help appreciated! =)


EDIT: I tried on Debian Linux now as well with javac 1.6.0_20 and java JRE 1.6.0_20-b02 - it works perfectly there - bug in the Mac version?

Last edited by crackinggear; 09-09-2010 at 11:08 AM. Reason: Tried on another platform
Old 09-10-2010, 04:58 AM   #2
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,199

Rep: Reputation: 1391Reputation: 1391Reputation: 1391Reputation: 1391Reputation: 1391Reputation: 1391Reputation: 1391Reputation: 1391Reputation: 1391Reputation: 1391
You are accessing variables from 2 different threads, therefore your program has undefined behaviour. The VM is probably caching the variable in a thread-local cache. Adding statements (the dummy statement) may cause the system not to cache the variable. A different system may decide not to cache the variable. If you declare the variable volatile it will never be cached. See Typical use of volatile in Java.
1 members found this post helpful.
Old 09-10-2010, 02:52 PM   #3
LQ Newbie
Registered: Jan 2003
Location: Sweden
Distribution: Ubuntu Server
Posts: 27

Original Poster
Rep: Reputation: 15
You're great! =)

Thank you very, VERY much!

I learn something new every day... =)


flag, flags, java, variables

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to lock files using fcntl() and to work between threads of the same process mistretzu Programming 6 11-05-2007 12:45 PM
Java does not work, I DLed Java and it still does not work M$ISBS Linux - Newbie 0 04-15-2007 08:20 PM
Question about threads (I don't understand how they work) zahadumy Programming 10 12-13-2005 12:19 PM
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
I know there are lots of threads on this but I REALLY cant work it out Boggit Linux - Newbie 8 03-30-2004 01:53 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:44 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration