• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread doubt

 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Master{
boolean bContinue=false;
public static void main(String argv[]){
Master m = new Master();
m.go();
}
public void go(){
Slave s = new Slave(this);
Thread t1 = new Thread(s);
t1.start();
while(bContinue==false){
System.out.println("loop");
}
s.setPrice(200);
}
}

class Slave implements Runnable{
int iPrice =100;
Master master;
Slave(Master m){
master=m;
}
synchronized public void setPrice(int iM){
iPrice=iM;
}

synchronized public void run(){
master.bContinue=true;
while(true){
System.out.println(iPrice);
}

}
}

i am getting as 100 as n number of times.
Please explain the above program.
 
Bijendra S. Rajput
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

<b>t1.start();<b>

when this line is executed

it will call the run() method...and which will run infinite times.
thats why you are getting 100 n times because this is the value of iPrice which run() method is printing.
 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MyRunnable implements Runnable {
String str = null;
MyRunnable(String s) {
str = s;
}
public void run() {
System.out.println(str);
}
}

public class Thread12 {
public static void main(String[] args) {
new Thread(new MyRunnable("runnable..")).start();
System.out.println("main thread");
}
}
op is
main thread
runnable..

here MyRunnable run method is not called immediately after the start call the thread.

In my first sample code, after start() ,while (bContinue == false) {} block is written.
I thought this loop will be executed.
How the thread run() method is called immediately after the start() call that thread.
 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MyRunnable implements Runnable {
String str = null;
MyRunnable(String s) {
str = s;
}
public void run() {
System.out.println(str);
}
}

public class Thread12 {
public static void main(String[] args) {
new Thread(new MyRunnable("runnable..")).start();
System.out.println("main thread");
}
}
op is
main thread
runnable..

here MyRunnable run method is not called immediately after the start call the thread.

In my first sample code, after start() ,while (bContinue == false) {} block is written.
I thought this loop will be executed.
How the thread run() method is called immediately after the start() call that thread.
 
Bijendra S. Rajput
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How the thread run() method is called immediately after the start() call that thread.

start is a method in the Thread class and when we call this method. It call the run() method, which is depend on your code which run() method will be called, means if we are not writing any run() method in our code then Thread class's run() method will be called, but we overwrite the run() method in our code and we are the work which we want to do inside the the run() method and it will be called, when this line will be executed.

t1.start();
 
Henry Wong
author
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here MyRunnable run method is not called immediately after the start call the thread.

In my first sample code, after start() ,while (bContinue == false) { } block is written.
I thought this loop will be executed.
How the thread run() method is called immediately after the start() call that thread.


Your run() method is called by the newly created thread immediately. It just takes time to create and schedule this new thread. The question shouldn't be "how the thread run() method is called immediately", but "why do you assume that the main thread will wait?".

Henry
 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my second sample output is
main thread
runnable..

here main() method thread is completed first, before the started thread.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The order in which runnable threads are chosen to run is not guaranteed.

This means that you don't know which threat wil run in your example and this will differ depending on the JVM and/or machine.

I have running your code a couple of times and sometimes I get 200 multiple times. The threads have the same priority.

When I give the new thread t1 a higher priority than the tread main then I always get 100.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic