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

java 第57节 线程调度的三个方法

时间:2016-07-01 21:30:57      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

2016-07-01

package com.java1995;

import java.util.Date;

/**
 * 休眠方法sleep()
 * @author Administrator
 *
 */
public class TestSleep {
    
    public static void main(String[] args) {
        
//        for(int i=0;i<10;i++){
//            System.out.println(i);
//            try {
//                Thread.sleep(200);
//            } catch (InterruptedException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
//        }
        
        while (true){
            System.out.println(new Date());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }

}

 

package com.java1995;
/**
 * 挂起方法join()
 * @author Administrator
 *
 */
public class TestJoin {
    
    public static void main(String[] args) {
        MyThread mt=new MyThread();
        mt.start();
        
        for(int i=0;i<10;i++){
            if(i==5){
                try {
                    mt.join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println("*********");
        }
    
    }

}

class MyThread extends Thread{
    
    public void run(){
        for(int i=0;i<10;i++){
            System.out.println("+++++++++++++++++++");
        }
    }
}

 

package com.java1995;
/**
 * 暂停方法yield()
 * @author Administrator
 *
 */
public class TestYield {
    
    public static void main(String[] args) {
        Thread t1=new Thread(new MyRunnable1());
        Thread t2=new Thread(new MyRunnable2());
        
        t1.start();
        t2.start();

    }

}

class MyRunnable1 implements Runnable{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i=0;i<200;i++){
            System.out.print("+");
            Thread.yield();
        }
    }    
}

class MyRunnable2 implements Runnable{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i=0;i<200;i++){
            System.out.print("*");
            Thread.yield();
        }
    }
}

 

java 第57节 线程调度的三个方法

标签:

原文地址:http://www.cnblogs.com/cenliang/p/5634269.html

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