LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

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


Hi!

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.java - main-method
Code:
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 T2.java
Code:
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! =)

//Nicholas

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
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,395

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814
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
crackinggear
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... =)
 
  


Reply

Tags
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


All times are GMT -5. The time now is 05:35 AM.

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