Results 1 to 10 of 10
Thread: The right way of using threads
- 02-20-2011, 02:26 PM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
The right way of using threads
Hi!
I have just started using threads and... confused myself :D
This is what I am trying to do:
- start the first thread, if level is 0 then level++, start another new thread, print out a counter every 1 second ,level++ every 5 seconds.
- in the newly created second thread it should print out a second counter and that counter should go faster and faster every 5 seconds (based on the level variable that is set by the first thread).
But what happens is it goes into the first thread and then it goes into the second thread and totally ignores the first thread/first counter.
So my guess is I am doing this completely wrong / using threads wrong.
Advise please!
my code:
Java Code:int levelCounter=0; int secondThreadTime; public void secondThread(){ int j=0; Thread secThread = new Thread(this); while(true){ try {secThread.sleep(secondThreadTime-(levelCounter*100));System.out.println("--"+j+"-"+levelCounter+"-");} catch(InterruptedException e){}; j++;} } public void run() {int i=0; while (true) { System.out.print(""+i); updateCounter(); //label1.setText(""+i); try {Thread.sleep(1000);} catch(InterruptedException e){}; i++; if(levelCounter<=0){ levelCounter++; secondThreadTime=1000; secondThread(); } if(i % 5 == 0){levelCounter++;System.out.println("*"+levelCounter+"*");} } }
-
You don't call sleep(...) on a Thread object as you're trying to do since sleep is a static method and only causes the currently running thread to sleep no matter which Thread object it's called on (and it should be only called on the class so that this is not ambiguous). You need to call start on threads for them to run as threads though. Have you gone through the threading tutorials?
- 02-20-2011, 02:40 PM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
> Have you gone through the threading tutorials?
Nope. Just an online 10-20 min intro to threads video tut.
Which tuts are you talking about?
-
- 02-20-2011, 02:44 PM #5
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Thanks for the link, will go through it , redo what I wrote on top and if problems will post back ;)
Cheers!
-
Best of luck, and let us know how it goes one way or the other.
- 02-20-2011, 02:53 PM #7
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Thanks! And will do! :)
- 02-20-2011, 03:58 PM #8
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Ok, I think I got it... but have one question (which I will ask at the end of this post).
Here's what I did (and it works to a certain extent) :
I created 4 test classes:
Java Code:public class TestFour { static int theLevel=0; public void setTheLevel(int i) { theLevel=i; } public int getTheLevel() { return theLevel; } }Java Code:public class TestThree implements Runnable{ TestFour tf=new TestFour(); public TestThree(int i){ /*TestFour tf=new TestFour(); System.out.println("rr"+tf.getTheLevel()); tf.setTheLevel(i); System.out.println(tf.getTheLevel()); System.out.println();*/ } public void run() { int j=0; Thread secThread = new Thread(this); while(true){ try { if(tf.getTheLevel()==0){ secThread.sleep(1000); }else{ int sleepingTime = 1000 -(tf.getTheLevel() * 100); if(sleepingTime==0){ System.out.println("BINGO!"); }else{ secThread.sleep(sleepingTime); } } System.out.println("--"+j+"-"+tf.getTheLevel()+"-"); } catch(InterruptedException e){}; j++;} } }and the main classJava Code:public class TestTwo implements Runnable{ TestFour tf=new TestFour(); public TestTwo(int i){ /* System.out.println("r "+tf.getTheLevel()); tf.setTheLevel(i); System.out.println(tf.getTheLevel()); System.out.println();*/ } public void run() { int i=1; while (true) { System.out.print(""+i); try {Thread.sleep(1000);} catch(InterruptedException e){}; i++; if(i % 5==0){ int a=tf.getTheLevel()+1; tf.setTheLevel(a); } } } }
My question, in class TestThree as you can see once the sleep time is at zero I print BINGO!, but how do make it break out of that loop?Java Code:public class TestOne { public static void main(String[] args) { Thread t1 = new Thread(new TestTwo(1)); Thread t2 = new Thread(new TestThree(10)); t1.start(); t2.start(); //TestTwo t2 = new TestTwo(1); //TestThree t3 = new TestThree(10); } }
(I know I can just reset the level/time - but is it possible to kill that thread totally?)
- 02-20-2011, 04:07 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,407
- Blog Entries
- 7
- Rep Power
- 17
- 02-20-2011, 04:39 PM #10
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Similar Threads
-
Threads and UML
By JUser in forum Advanced JavaReplies: 0Last Post: 09-27-2010, 08:43 PM -
Threads
By Tanuck in forum New To JavaReplies: 5Last Post: 09-21-2010, 02:44 AM -
When to use threads
By simorgh in forum Threads and SynchronizationReplies: 2Last Post: 02-12-2010, 07:43 AM -
GUI and Threads
By rp181 in forum Threads and SynchronizationReplies: 1Last Post: 10-10-2009, 08:39 PM -
Threads
By one198 in forum Threads and SynchronizationReplies: 1Last Post: 11-20-2007, 06:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks