dcsimg
why does java behave differently on different machines
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   vijay_vikram
Posted On:   Saturday, July 14, 2001 07:00 AM

dear sir, i have some unanswered questions in java and would be very happy if you can give a reply to them q1.can we write an executable (say helloworld.exe ) with java q2.the following code demonstrates the thread using priorities.the thread are allowed to run for 5 seconds and each thread executes a loop ,counting the iterationsand then it is displayed. class clicker implements Runnable { int click=0; Thread t; private boolean running=true; public clicker(int p) { t= new Thread(this); t.setPriority(p); } public void run() { while(running) { click++; } } public v   More>>

dear sir,

i have some unanswered questions in java and would be very happy if you can give a reply to them



q1.can we write an executable (say helloworld.exe ) with java



q2.the following code demonstrates the thread using priorities.the thread are allowed to run for 5 seconds and each thread executes a loop ,counting the iterationsand then it is displayed.


			
class clicker implements Runnable {
int click=0;
Thread t;
private boolean running=true;

public clicker(int p) {
t= new Thread(this);
t.setPriority(p);
}
public void run() {
while(running) {
click++;
}
}
public void stop() {
running=false;
}
public void start() {
t.start();
}
}

class thread {
public static void main(String args[]) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
clicker hi=new clicker(Thread.NORM_PRIORITY+2);
clicker lo=new clicker(Thread.NORM_PRIORITY-2);

hi.start();
lo.start();
try {
Thread.sleep(5000);
}
catch(InterruptedException ie) {
System.out.println("Main thread interrupted");
}
lo.stop();
hi.stop();

//wait for child thread to terminate
try{
hi.t.join();
lo.t.join();
} catch(InterruptedException ie) {
System.out.println("interruptedexception caught");
}

System.out.println("low priority thread"+lo.click);
System.out.println("high priority thread"+hi.click);
}
}


however after executing them in

i] WIN 95 ,the output is
low priority is 434104

the high priority is 4860791


ii] WIN 98, the output is
low priority is 8321040

the high priority is 191484604


iii] on SOLARIS, the output is
low priority is 0

the high priority is 3062507



now the question why does java behave in a confusing manner in these OS(why different values at output?)    <<Less

Re: why does java behave differently on different machines

Posted By:   Alex_Chaffee  
Posted On:   Monday, July 16, 2001 12:56 AM

1. See the JavaLanguage FAQ


2. Java thread scheduling behavior is undefined for precisely this reason -- different OS's behave differently. RTFM for more info.

Re: why does java behave differently on different machines

Posted By:   Luigi_Viggiano  
Posted On:   Saturday, July 14, 2001 03:03 PM

Threads hanling is demanded to the OS; different OS manages threads differently.
It seems to me that if you execute this code two times on the same machine you can get different output very easily.
About | Sitemap | Contact