Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Controlling individual threads

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

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


    ############# Start 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;
    	}
    }
    ################## End Java Code #######################

    Thank you for any ideas.
    Youngstorm
    Last edited by youngstorm; October 18th, 2010 at 02:39 PM. Reason: Inserted code tags


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Controlling individual threads

    This isn't a thread problem per se but rather an accumulation of several syntax errors. First, you are abstracting the threadTest class to a Thread, which does not contain the methods you are calling. Second, threadTest class does not contain these methods, but Thread1 does. Thirdly, your t1/t2 variables in the threadTest.run method are out of scope.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Controlling individual threads

    Thank you for taking the time to respond.

    First, you are abstracting the threadTest class to a Thread, which does not contain the methods you are calling. Second, threadTest class does not contain these methods, but Thread1 does.
    How do I go about properly using these methods?

    Thirdly, your t1/t2 variables in the threadTest.run method are out of scope.
    I;m not sure I really understand this part, as it relates to threads. If I move the thread creation statements out of main, I get more errors than before. The new ones are as follows.

    ######################

    threadTest.java:11: non-static variable t1 cannot be referenced from a static context
    t1.start();
    ^
    threadTest.java:13: non-static variable t1 cannot be referenced from a static context
    while (t1.getcalcDone() == false); //This loop will run till thread 1 has made it's calculation.

    ######################

    Again, I may be thinking about threads all wrong. Sorry if I'm causing utter confusion.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Controlling individual threads

    Again, this isn't a thread issue, its a syntax and programming flow problem.

    threadTest t1 = new threadTest();
    This allows the reference t1 access to the methods in threadTest. This will not fix your problem however, since you cannot call the methods defined in the class Thread1 from a reference (t1).

    I suggest you read some beginning tutorials on the subjects of variable scope and how to define an object and write a class before diving into multi-threading.

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Controlling individual threads

    Thanks. I'll do that and have some already.

Similar Threads

  1. newbie help threads
    By fortune2k in forum Threads
    Replies: 19
    Last Post: October 20th, 2010, 02:40 PM
  2. How Can I run java threads
    By Saeid in forum Threads
    Replies: 12
    Last Post: August 6th, 2010, 02:12 PM
  3. Working with threads
    By tccool in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 12th, 2010, 10:21 AM
  4. threads in gui
    By urosz in forum AWT / Java Swing
    Replies: 1
    Last Post: November 3rd, 2009, 05:20 PM
  5. Replies: 2
    Last Post: February 4th, 2009, 12:24 PM