Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in wait() method.......

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.......

 
karthik sekar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.......

 
karthik sekar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.......

 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some differentiations when you want to run a thread that has been extended from Thread class and one which has implemented the Runnable.
Go through this code and I guess it would be clear. For a class that implements runnable, to start the thread, we always need a runnable target.
Since the class that extends java.lang.Thread already implements runnable, we can start the thread simplpy by creating an object of the class and calling start method on it.

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i got it.
thanks a lot paul.
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karthik,
Welcome to JavaRanch..
If you have problems replying to a post then there are two options:

1:


2:
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes karthik and if you quote someone's post, do add your own content to it...

seems like paul is using google chrome .....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic