Results 1 to 4 of 4
  1. #1
    youngstorm is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default Controlling individual threads

    Hi, I am new to threads and have made a test program. The code is short. I am trying to control each thread. I am not sure if my method is right. Can someone please help? I can not find the answer in books or on the internet, though, it could be my own mindset. I am getting the following error:

    ############# Start javac output #########################
    exec: javac -g threadTest.java

    threadTest.java:13: cannot find symbol
    symbol : method getcalcDone()
    location: class java.lang.Thread
    while (t1.getcalcDone() == false); //This loop will run till thread 1 has made it's calculation.
    ^
    threadTest.java:21: cannot find symbol
    symbol : variable t1
    location: class threadTest
    t1.calc(5, 3); // This SHOULD add 5 and 3 together. Not sure if I can control individual threads like this.
    ^
    threadTest.java:22: cannot find symbol
    symbol : variable t1
    location: class threadTest
    t2.calc(t1.results, 10); // Add the results of the first calculation and 10 together.
    ^
    threadTest.java:22: cannot find symbol
    symbol : variable t2
    location: class threadTest
    t2.calc(t1.results, 10); // Add the results of the first calculation and 10 together.
    ^
    4 errors
    ############# End javac output #########################

    Java Code:
    import java.util.*;
    
    public class threadTest extends Thread
    {
    
    	public static void main(String[] args) 
    	{
    		Thread t1 = new threadTest();
    		Thread t2 = new threadTest();
    		
    		t1.start();
    		
    		while (t1.getcalcDone() == false); //This loop will run till thread 1 has made it's calculation.
    		
    		t2.start();		
    		
    	}
    	
    	public void run()
       {
    	   t1.calc(5, 3); // This SHOULD add 5 and 3 together. Not sure if I can control individual threads like this.
    		t2.calc(t1.results, 10); // Add the results of the first calculation and 10 together.
    	}
    
    }
    
    
    class Thread1 extends Thread
    {
       int a = 0;
    	int b = 0;
    	int results = 0;
    	boolean calcDone = false;
    	
       public Thread1(int a, int b)
    	{
    	    this.a = a;
    		 this.b = b;
    	}
    
       void calc(int a, int b)
    	{
    	    results = a + b;
    		 calcDone = true;
    	}
    
       boolean getcalcDone()
    	{
    	    return calcDone;
    	}
    }
    Thank you for any ideas.
    Youngstorm

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

    Default

    The error is telling you that it can't find symbols, for instance it can't find the method getcalcDone() when you call it on t1. So check out the t1 variable to see what kind of variable it is -- and in fact it's a Thread variable. Since this method doesn't exist for Thread objects or variables, you can see why the compiler is complaining. In fact t1 holds a threadTest object, but this doesn't have this method either.

    So which class does have this method? When you figure this out, declare t1 as a variable of this class and call the new operator on this class as well, and this should at least get you started towards a solution.

    Luck!

  3. #3
    youngstorm is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    Thank you for your help. I have changed my code and it works better now.

    Java Code:
    import java.util.*;
    
    public class threadTest extends Thread
    {
       
        public static void main(String[] args) 
        {
           Thread1 t1 = new Thread1();
           Thread1 t2 = new Thread1();
        
            t1.start();
            
            while (t1.getcalcDone() == false); //This loop will run till thread 1 has made it's calculation.
            
            t2.start();        
            
        }
    }
    
    
    class Thread1 extends Thread
    {
       int a = 0;
        int b = 0;
        int results = 0;
        boolean calcDone = false;
        
       public void run()
       {
            calc(5, 3);
       }
        
       void calc(int a, int b)
        {
            results = a + b;
             calcDone = true;
        }
    
       boolean getcalcDone()
        {
            return calcDone;
        }
    }
    If I comment out the calc() call in the run() method, the while loop becomes infinite, as it should. Do you see anything else I may be doing wrong so far?

    Again, thanks for your help.
    Youngstorm

  4. #4
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    You can control; but it is not a good way. A thread is created to handle a task independent of other tasks. If there are dependencies across tasks, there should not be thread for it. sharing data across thread is not safe and can lead to undesirable results. But, if you want to wait for thread to complete to proceed, try using .join() method.

    I will recommend read more on thread and java concurrency package

Similar Threads

  1. * vs. individual package name?
    By XmisterIS in forum New To Java
    Replies: 3
    Last Post: 09-01-2010, 01:19 PM
  2. Controlling cmd from java...HELP!!
    By eponcedeleon in forum Advanced Java
    Replies: 12
    Last Post: 02-23-2010, 07:33 PM
  3. Calculating Individual Numbers
    By TheKnight in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 01:51 AM
  4. Combining Individual Sounds - Urgent
    By JDCAce in forum Advanced Java
    Replies: 4
    Last Post: 12-05-2008, 06:17 AM
  5. controlling GC
    By ravian in forum Eclipse
    Replies: 2
    Last Post: 01-03-2008, 09:13 AM

Posting Permissions

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