This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
Joined: Sep 19, 2006
Posts: 41
posted
0
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.
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.
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
Joined: Sep 19, 2006
Posts: 41
posted
0
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.
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?".