aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread doubt" Watch "Thread doubt" New topic
Author

Thread doubt

sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
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
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.


Thanks <br /> <br />Regards,<br />------------------------------<br />Bijendra S. Rajput<br />SCJP 1.5<br />------------------------------
sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
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

Joined: Oct 23, 2006
Posts: 74
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

Joined: Sep 19, 2006
Posts: 41
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
in my second sample output is
main thread
runnable..

here main() method thread is completed first, before the started thread.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
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.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
 
 
subject: Thread doubt