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