??? ?? 2017-10-12 01:55 采纳率: 100%
浏览 796
已采纳

java之Thread线程相关 代码案例代码案例

有没有yield()、sleep()、wait()、join()、run和start区别代码实例代码案例 找不到  可以运行的 不要 文档
  • 写回答

1条回答 默认 最新

  • 来时路去征途 2017-10-12 02:11
    关注

    这是我做练习的时候写的代码,你可以参考下,
    /**
    *run(),start(),join(),sleep(),yield();练习
    */
    public class ThreadTest {
    private int i=0;
    private Object o = new Object();
    static class Mythread extends Thread{
    public void run() {
    System.out.println("0当前线程ID:"+Thread.currentThread().getId());
    }
    }

    static  class  Mythread1 implements Runnable{
        public void run() {
            System.out.println("1当前线程ID:"+Thread.currentThread().getId());
        }
    }
    
    public static void main(String[] args) throws InterruptedException {
        /*System.out.println("主线程:"+Thread.currentThread().getId());
        Mythread mythread = new Mythread();
        mythread.run();
    
        mythread.start();
        System.out.println("******************************************************");
    
        System.out.println("主线程:"+Thread.currentThread().getId());
        Mythread1 mythread1 = new Mythread1();
        mythread1.run();
        Thread thread = new Thread(mythread1);
        thread.start();*/
        //mythread1.start();
        ThreadTest test = new ThreadTest();
        /*SleepClass p =test.new SleepClass();
        SleepClass p1 = test.new SleepClass();
        p.start();
        p1.start();*/
        /*YeildClass y = test.new YeildClass();
        y.start();*/
    
        //启动子线程
        new JoinClass("new  Thread").start();
        for(int i=0;i<10;i++){
            if(i==5){
                Thread th = new JoinClass("thead");
                th.start();
                th.join();
            }
            System.out.println(Thread.currentThread().getName()+" "+i);
        }
    }
    
    class SleepClass extends Thread{
        @Override
        public void run() {
            synchronized (o){
                i++;
                System.out.println("i:"+i);
                try {
                    System.out.println("线程:"+ Thread.currentThread().getName()+"进入睡眠");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程"+Thread.currentThread().getName()+"睡眠结束");
                i++;
                System.out.println("i:"+i);
            }
        }
    }
    
    class YeildClass extends Thread{
        public void run() {
            long benginTime = System.currentTimeMillis();
            int count = 0;
            for(int i=0;i<500000;i++){
                count = count +i;
                Thread.yield();
            }
            long endTime = System.currentTimeMillis();
            System.out.println("用时:"+(endTime-benginTime));
        }
    }
    
    static class JoinClass extends Thread{
        public JoinClass(String name){
            super(name);
        }
        public void run() {
            for (int i =0;i<5;i++){
                System.out.println(this.getName()+" "+i);
            }
        }
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题