This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
thread why not to sleep
Posted by christoph on March 31, 2001 at 10:37 PM
I wrote a program .It want t1 thread go to sleep,but as I run this program,the results show it just run as many times as t2 and t3,why this happen.the program like this below:import java.lang.*; public class jth extends Thread { public static void main(String []args) { long j=0; Thread t1 = new Thread(new Runner()); Thread t2 = new Thread(new Run1()); Thread t3 = new Thread(new Run2()); // while (true) // { for(long i=0;i<100000000;i++); System.out.println("This is main thread"); //t1.setPriority(3); t1.start(); t2.start(); t3.start(); //int p = getPriority(); System.out.println("The t1 thread priority is "+t1.getPriority()); System.out.println("The t2 thread priority is "+t2.getPriority()); //System.out.println("The main thread priority is "+p); //t1.stop(); try{ System.out.println("t1 sleeping begin"); // t.join(); t1.sleep(100); // t2.sleep(100); // t3.sleep(200); System.out.println("t1 sleeping finish"); } catch (InterruptedException e) { System.out.println("interrupted exception"); } while (true) { j++; System.out.println("main is running:"+j); } // t1.yield(); } } class Runner implements Runnable { private int l=0; public void run() { //for (int i=0;i<5;i++) while (true) { //for(long k=0;k<1000;k++); l++; System.out.println("This is '111' thread:::"+l); } } } class Run1 implements Runnable { int m=0; public void run() { while (true) { //for(long k=0;k<1000;k++); m++; System.out.println("This is '222' thread:::"+m); } } } class Run2 implements Runnable { private int n=0; public void run() { while (true) { //for(long k=0;k<1000;k++); n++; System.out.println("This is '333' thread:::"+n); } } }
Replies:
|