Results 1 to 10 of 10
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default 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+"*");}
    	           
    	       }
    	    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  3. #3
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    > 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?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Thanks for the link, will go through it , redo what I wrote on top and if problems will post back ;)

    Cheers!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Thanks! And will do! :)

  8. #8
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    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++;}
    	 }
    
    
    }
    Java 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);
    	           }
    			}
    		}
    }
    and the main class
    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);
    		
    	}
    
    }
    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?
    (I know I can just reset the level/time - but is it possible to kill that thread totally?)

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by N00Bie View Post
    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?
    (I know I can just reset the level/time - but is it possible to kill that thread totally?)
    Simply return from the run() method which immediately brings that Thread in the 'stopped' state.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

Similar Threads

  1. Threads and UML
    By JUser in forum Advanced Java
    Replies: 0
    Last Post: 09-27-2010, 09:43 PM
  2. Threads
    By Tanuck in forum New To Java
    Replies: 5
    Last Post: 09-21-2010, 03:44 AM
  3. When to use threads
    By simorgh in forum Threads and Synchronization
    Replies: 2
    Last Post: 02-12-2010, 08:43 AM
  4. GUI and Threads
    By rp181 in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-10-2009, 09:39 PM
  5. Threads
    By one198 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-20-2007, 07:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •