码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA_Join_Yield_Priority

时间:2015-05-11 23:42:37      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

 1 public class TestJoin {
 2   public static void main(String[] args) {
 3     MyThread2 t1 = new MyThread2("abcde");
 4     t1.start();
 5     try {
 6         t1.join();
 7     } catch (InterruptedException e) {}
 8         
 9     for(int i=1;i<=10;i++){
10       System.out.println("i am main thread");
11     }
12   }
13 }
14 class MyThread2 extends Thread {
15   MyThread2(String s){
16       super(s);
17   }
18   
19   public void run(){
20     for(int i =1;i<=10;i++){
21       System.out.println("i am "+getName());
22       try {
23           sleep(1000);
24       } catch (InterruptedException e) {
25           return;
26       }
27     }
28   }
29 }

 

 1 public class TestYield {
 2   public static void main(String[] args) {
 3     MyThread3 t1 = new MyThread3("t1");
 4     MyThread3 t2 = new MyThread3("t2");
 5     t1.start(); t2.start();
 6   }
 7 }
 8 class MyThread3 extends Thread {
 9   MyThread3(String s){super(s);}
10   public void run(){
11     for(int i =1;i<=100;i++){
12       System.out.println(getName()+": "+i);
13       if(i%10==0){
14         yield();
15       }
16     }
17   }
18 }

 

 

技术分享

 

 1 public class TestPriority {
 2     public static void main(String[] args) {
 3         Thread t1 = new Thread(new T1());
 4         Thread t2 = new Thread(new T2());
 5         t1.setPriority(Thread.NORM_PRIORITY + 3);
 6         t1.start();
 7         t2.start();
 8     }
 9 }
10 
11 class T1 implements Runnable {
12     public void run() {
13         for(int i=0; i<1000; i++) {
14             System.out.println("T1: " + i);
15         }
16     }
17 }
18 
19 class T2 implements Runnable {
20     public void run() {
21         for(int i=0; i<1000; i++) {
22             System.out.println("------T2: " + i);
23         }
24     }
25 }

 

JAVA_Join_Yield_Priority

标签:

原文地址:http://www.cnblogs.com/roger-h/p/4495954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!