package day1112; public class Runner4 extends Thread { @Override public void run(){ for(int i = 0;i<10;i++){ System.out.println(this.currentThread()+"_____________"+i); } } public static void main(String[] args) { // Runner4 r = new Runner4(); // r.start(); Runner5 r = new Runner5(); Thread t1 = new Thread(r,"板桥"); Thread t2 = new Thread(r,"烧火棍"); System.out.println(t1.getName()); System.out.println(t1.getId()); System.out.println(t2.getName()); System.out.println(t2.getId()); t1.start(); t2.start(); } } class Runner5 implements Runnable{ @Override public void run(){ for(int i = 0 ; i < 10;i++){ System.out.println(Thread.currentThread()+"___________"+i); } } }
package day1112; import java.util.Date; public class SleepTesting extends Thread{ private byte flag = 0; @Override public void run(){ while(true){ System.out.println(this.currentThread()+"________"+new Date()); flag++; if(flag >= 10){ return; // 标识变量,结束线程 } try { sleep(1000); } catch (InterruptedException e) { System.out.println("什么情况"); return; //通过结束方法来结束线程 } } } public static void main(String[] args) throws InterruptedException { SleepTesting t1 = new SleepTesting(); SleepTesting t2 = new SleepTesting(); t1.start(); t1.setName("板桥"); t2.start(); t2.setName("烧火棍"); t1.interrupt(); // t1.stop(); //结束线程(不使用),太过生硬 Thread.sleep(6000); System.out.println(t1.isAlive()); System.out.println(t2.isAlive()); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)