• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .....
 
I am displeased. You are no longer allowed to read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic