File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in wait() method....... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in wait() method......." Watch "doubt in wait() method......." New topic
Author

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

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
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.......



SCJP5 and SCWCD1.5
Think Twice Act Wise...
karthik sekar
Greenhorn

Joined: Oct 15, 2008
Posts: 2
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

Joined: Oct 15, 2008
Posts: 2
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

Joined: May 19, 2008
Posts: 177
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.



Preparing for SCJP 6.0
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
yeah i got it.
thanks a lot paul.
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
Hi Karthik,
Welcome to JavaRanch..
If you have problems replying to a post then there are two options:

1:


2:
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Yes karthik and if you quote someone's post, do add your own content to it...

seems like paul is using google chrome .....


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in wait() method.......