Threads
Hey All. I think this is a somewhat rather trivial problem, but i've been staring at it for a few hours now.
Below, i can't seem to output the correct value for the variable 'num'. I know that the 'void Child(int n)' method in the 'Child' class isn't being called for some reason, hence i cannot get the correct output for 'num'. when i compile the code the output is the following:
Parent has created two children.
Parent has rejoined Child 1.
This is Child 0
Parent has rejoined Child 2.
This is Child 0
'This is Child 0' should have the values 1 and 2 respectively. I get zero twice.
import java.lang.Thread;
publicclass Parentextends Thread{
publicstaticvoid main(String args[]){
Parent p =new Parent();
Child one =new Child();
Child two =new Child();
System.out.println("Parent has created two children.");
one.start();
try{
p.join();
}catch (InterruptedException e){}
System.out.println("Parent has rejoined Child 1.");
two.start();
try{
p.join();
}catch (InterruptedException e){}
System.out.println("Parent has rejoined Child 2.");
}
}
class Childextends Thread{
int num;
void Child(int n){
n = 0;
int num = n;
n++;
}
publicvoid run(){
System.out.println("This is Child " +num);
}
}
[2746 byte] By [
Sandy7a] at [2007-11-26 17:10:24]

Well as far as I can see, there is nothing in that code that tells void Child to be called, i think what you mean is instead of it being a void, you want it to be a constructor, ie. it will be called whenever a child object is created, in that case make your code something like this:
import java.lang.Thread;
public class Parent extends Thread {
public static void main(String args[]) {
Parent p = new Parent();
Child one = new Child(1);
Child two = new Child(2);
System.out.println("Parent has created two children.");
one.start();
try {
p.join();
} catch (InterruptedException e) {}
System.out.println("Parent has rejoined Child 1.");
two.start();
try {
p.join();
} catch (InterruptedException e) {}
System.out.println("Parent has rejoined Child 2.");
}
}
class Child extends Thread {
int num;
Child(int n) {
num = n;
}
public void run() {
System.out.println("This is Child " +num);
}
}
I'm not quite sure what you're doing with the n and num but I think that will work.
thank you very much, that worked a treat. Sorry, but could you briefly explain why it wasn't working before and is now?
It's just that i was given the following brief and now i am a little confused as it requires the child method to be type void:
The classes Parent and Child both extend the class
java.lang.Thread
Child
--
has a constructor method
void Child(int n)
which stores the initial parameter "n" in a local variable "num"
and has a
public void run()
method which
- prints the string
"This is Child "+num
- ends
Parent
has a
public static void main(String args[])
method which
- creates two children Child(1) and Child(2)
- prints the string "Parent has created two children."
- starts Child 1
- joins to Child 1
- prints "Parent has rejoined Child 1."
- starts Child 2
- joins to Child 2
- prints "Parent has rejoined Child 2."
- ends.
num is always zero in your current code.p.join() does nothing because p hasn't started.It even does not have its run() method.Read the basic tutorial: http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html
hiwaa at 2007-7-8 23:38:14 >

thanks for the reply. I want to be able to join Parent p to Child 1 and Child 2. How would i go about doig this? I have at the API and the java tutorial on threads and have only found the 'join' method to be the one i think is correct. I've implemented p.join(one); but this hasn't worked. if you could even help point me in the right direction i would be ever so grateful.
sandster,post the current version of your code and we'll have a geezer.keith.
> I want to be able to join Parent p to Child 1 and Child 2
What do you mean by the word 'join'?
Tell us its meaning, or, semantics.
If your really mean the Thread.join() method, do as follows:
1a)Make a Parent instance, call it p1, in a Child 1 instance c1.
1b)Make a Parent instance, call it p2, in a Child 2 instance c2.
2a)Start p1 in the c1.
2b)Start p2 in the c2.
3a)Call p1.join() in the c1 before p1 ends.
3b)Call p2.join() in the c2 before p2 ends.
hiwaa at 2007-7-8 23:38:14 >

Hey, the current code is:
import java.lang.Thread;
public class Parent extends Thread {
public static void main(String args[]) {
Parent p = new Parent();
Child one = new Child(1);
Child two = new Child(2);
System.out.println("Parent has created two children.");
one.start();
try {
p.join();
} catch (InterruptedException e) {}
System.out.println("Parent has rejoined Child 1.");
two.start();
try {
p.join();
} catch (InterruptedException e) {}
System.out.println("Parent has rejoined Child 2.");
}
}
class Child extends Thread {
int num;
Child(int n) {
num = n;
}
public void run() {
System.out.println("This is Child " +num);
}
}
I want to be able to take the newly created Parent p thread and have it join to both Child 1 and Child 2. Didn't quite understand what the last poster mean't, english was pretty bad.
this is what i require:
Parent Class
that has a
public static void main(String args[])
method which
- creates two children Child(1) and Child(2)
- prints the string "Parent has created two children."
- starts Child 1
- joins to Child 1
- prints "Parent has rejoined Child 1."
- starts Child 2
- joins to Child 2
- prints "Parent has rejoined Child 2."
- ends.
so basically, i want to create ONE parent instance, which starts and joins to both Child 1 and Child 2. There should not be more than one instance of Parent. In my code, Parent p should start Child 1, then join to it. For Child 2, it should start it and then join to it.
> which starts and joins to both Child 1 and Child 2If your 'join' is Thread.join() method, then, p.join() should be called from within Child1 and Child2. Read my previous reply carefully.
hiwaa at 2007-7-8 23:38:14 >

sorry, i really really dont mean to be stupid but how would i create a thread within another thread? i've googled around out for help but to no avail so far...
> sorry, i really really dont mean to be stupid but how
> would i create a thread within another thread? i've
> googled around out for help but to no avail so far...
class ThreadA extends Thread{
public void run(){
...
}
}
class ThreadB extends Thread{
public void run(){
ThreadA ta = new ThreadA();
ta.start();
...
}
}
hiwaa at 2007-7-8 23:38:14 >

I think you are looking for something like this. Parent doesn't need to extend Thread. You should be calling join() on the Child objects, not the parent. I've added a configurable sleep() call to the run() of Child so that you can see the output better as it executes. Child 1 sleeps for 2 secods, Child 2 for 3 seconds. If you watch the output, you'll see that after "This is Child 2" is printed, there is a 2 second delay before "Parent has rejoined Child 1" is printed, and then another 1 second delay (because 2 seconds passed while waiting for Child 1 to rejoin) before "Parent has rejoined Child 2" is displayed.
The output now looks as you would expect:
Parent has created two children.
This is Child 1
This is Child 2
Parent has rejoined Child 1.
Parent has rejoined Child 2.
This is what your code now looks like:
BTW - You do not need to import anything from java.lang package.
public class Parent {
public static void main(String args[]) {
Parent p = new Parent();
Child one = new Child(1, 2000);
Child two = new Child(2, 3000);
System.out.println("Parent has created two children.");
one.start();
two.start();
try {
one.join();
}
catch (InterruptedException e) {
}
System.out.println("Parent has rejoined Child 1.");
try {
two.join();
}
catch (InterruptedException e) {
}
System.out.println("Parent has rejoined Child 2.");
}
private static class Child extends Thread {
int childNumber;
long sleepTime;
Child(int childNumber, long sleepTime) {
this.childNumber = childNumber;
this.sleepTime = sleepTime;
}
public void run() {
System.out.println("This is Child " + childNumber);
try {
sleep(sleepTime);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
