karthik sekar

Greenhorn
+ Follow
since Oct 15, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by karthik sekar

Originally posted by Ganeshkumar cheekati:
class ThreadA {
public static void main(String [] args) {
ThreadB b = new ThreadB(); ---------------------------line 1
b.start(); ----------------------------line 2
synchronized(b) {
try {
System.out.println("Waiting for b to complete...");
b.wait();
} catch (InterruptedException e) {}
System.out.println("Total is: " +b.total);
}
}

class ThreadB extends Thread {
int total;
public void run() {
synchronized(this) {
for(int i=0;i<100;i++) {
total += i;
}
notify();
}
}
}

my doubt is in line 1 and line 2.

we have to start thread like

ThreadB b=new ThreadB(); ------creating object for class

Thread t=new Thread(b); ------thread object is created

instead of this

directly creating object for ThreadB as well as calling the start() method.

why we are doing like this?

can you explain it? thanks in advance.......

Originally posted by Ganeshkumar cheekati:
class ThreadA {
public static void main(String [] args) {
ThreadB b = new ThreadB(); ---------------------------line 1
b.start(); ----------------------------line 2
synchronized(b) {
try {
System.out.println("Waiting for b to complete...");
b.wait();
} catch (InterruptedException e) {}
System.out.println("Total is: " +b.total);
}
}

class ThreadB extends Thread {
int total;
public void run() {
synchronized(this) {
for(int i=0;i<100;i++) {
total += i;
}
notify();
}
}
}

my doubt is in line 1 and line 2.

we have to start thread like

ThreadB b=new ThreadB(); ------creating object for class

Thread t=new Thread(b); ------thread object is created

instead of this

directly creating object for ThreadB as well as calling the start() method.

why we are doing like this?

can you explain it? thanks in advance.......