• JavaSE 基础 第57节 线程调度的三个方法


    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();
            }
        }
    }
  • 相关阅读:
    I Hate It
    hdu 2112 HDU Today ( Dijkstra )
    hdu 1280 前m大的数
    hdu 4252 A Famous City
    hdu 2647 Reward
    hdu 2845 Beans
    hdu 3548 Enumerate the Triangles ( 优 化 )
    hdu 3552 I can do it! (贪心)
    HDU 3033 I love sneakers!(分组背包变形)
    hdu 1712 ACboy needs your help 分组背包
  • 原文地址:https://www.cnblogs.com/cenliang/p/5634269.html
Copyright © 2020-2023  润新知