Results 1 to 4 of 4
Thread: Controlling individual threads
- 10-18-2010, 08:39 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 4
- Rep Power
- 0
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 #########################
Thank you for any ideas.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; } }
Youngstorm
-
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!
- 10-19-2010, 10:14 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 4
- Rep Power
- 0
Thank you for your help. I have changed my code and it works better now.
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?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; } }
Again, thanks for your help.
Youngstorm
- 12-03-2010, 10:37 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
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
-
* vs. individual package name?
By XmisterIS in forum New To JavaReplies: 3Last Post: 09-01-2010, 12:19 PM -
Controlling cmd from java...HELP!!
By eponcedeleon in forum Advanced JavaReplies: 12Last Post: 02-23-2010, 06:33 PM -
Calculating Individual Numbers
By TheKnight in forum New To JavaReplies: 2Last Post: 01-30-2009, 12:51 AM -
Combining Individual Sounds - Urgent
By JDCAce in forum Advanced JavaReplies: 4Last Post: 12-05-2008, 05:17 AM -
controlling GC
By ravian in forum EclipseReplies: 2Last Post: 01-03-2008, 08:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks