Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

doubt in wait() method.......  RSS feed

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

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

 
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:
 
Sheriff
Posts: 9643
42
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 .....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!