线程 (4)线程创建 属性 id 名称 状态 优先级

在这里插入图片描述在这里插入图片描述

public class  ThreadDemo10 {
    public static void main(String[] args){
    Thread t=new Thread("线程1")
    {
        @Override
        public void run() {

            try{
                 Thread.sleep(60*60*1000);
            }catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    };
    t.start();}
}

打开jconsole
查看线程情况
在这里插入图片描述在这里插入图片描述

public class ThreadDemo11 {
    public static void main(String[] args)
    {
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
try {
    Thread.sleep(60*60*1000);
}catch (InterruptedException e){
    e.printStackTrace();
}
            }
        },"Runnable-Thread");
        thread.start();
    }
}

代码修改 重新打开jconsole 查看线程情况

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述`

import java.util.Random;

public class ThreadDemo12 {
    public static void main(String[] args)
    {
         ThreadGroup group=new ThreadGroup("thread-group");
         Runnable runTask=new Runnable() {
             @Override
             public void run() {
                 int num=(1+new Random().nextInt(3));
                 try{
                     Thread.sleep(num*1000);

                 }catch (InterruptedException e)
                 {e.printStackTrace();}
                 System.out.println("选手到达终点"+num+"s");
             }
         };
         Thread t1=new Thread(group,runTask);
         Thread t2=new Thread(group,runTask);
         Thread t3=new Thread(group,runTask);
         t1.start();
         t2.start();
         t3.start();

         while (group.activeGroupCount()!=0){

         }
        System.out.println("宣布成绩") ;
    }
}

在这里插入图片描述

import java.util.Random;

public class ThreadDemo12 {
    public static void main(String[] args)
    {//创建一个线程分组(创建一个比赛)
         ThreadGroup group=new ThreadGroup("thread-group");

       //定义一个公共的任务(线程任务)
         Runnable runTask=new Runnable() {
             @Override
             public void run() {//任务
                 int num=(1+new Random().nextInt(3));
                 try{//到达终点时间
                     Thread.sleep(num*1000);

                 }catch (InterruptedException e)
                 {e.printStackTrace();}
                 System.out.println("选手到达终点"+num+"s");
             }
         };
         //3 线程 运动员
         Thread t1=new Thread(group,runTask);
         Thread t2=new Thread(group,runTask);
         Thread t3=new Thread(group,runTask);
         //开炮

         t1.start();
         t2.start();
         t3.start();
//到达后宣布成绩
         while (group.activeGroupCount()!=0){

         }
        System.out.println("宣布成绩") ;
    }
}

在这里插入图片描述单线程名称

public class ThreadDemo13 {
    public static void main(String[] args)
    {
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                Thread t=Thread.currentThread();
                System.out.println("线程ID:"+t.getId());
                System.out.println("线程名称:"+t.getName());
            }
        },"线程1");
        thread.start();
    }
}

在这里插入图片描述多线程名称 状态

    public static void main(String[] args)
    {
Runnable runnable=new Runnable() {
    @Override
    public void run() {
            Thread t=Thread.currentThread();
            System.out.println("线程ID:"+t.getId());
            System.out.println("线程名称:"+t.getName());
        System.out.println();
    }
};
        Thread thread=new Thread(runnable,"线程1");
        thread.start();
        System.out.println();

        Thread thread2=new Thread(runnable,"线程2");
        thread2.start();
    }
}

在这里插入图片描述在这里插入图片描述加入时间

public class ThreadDemo13 {
    public static void main(String[] args) throws InterruptedException {
   Runnable runnable=new Runnable() {
    @Override
    public void run() {
            Thread t=Thread.currentThread();
            System.out.println("线程ID:"+t.getId());
            System.out.println("线程名称:"+t.getName());
    }
};
        Thread thread=new Thread(runnable,"线程1");
        thread.start();

       Thread.sleep(500);
        System.out.println();

        Thread thread2=new Thread(runnable,"线程2");
        thread2.start();
    }
}

线程状态

public class ThreadDemoBystate
{
    public static void main(String[] args)
    {
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {
              Thread thread=Thread.currentThread();
                System.out.println("线程状态2:"+thread.getState());
            }
        });
        System.out.println("线程的状态:"+t.getState());
        t.start();
    }
}

在这里插入图片描述

public class ThreadDemoBystate
{
    public static void main(String[] args) throws InterruptedException {
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {
              Thread thread=Thread.currentThread();
                System.out.println("线程状态2:"+thread.getState());
            }
        });
        System.out.println("线程的状态:"+t.getState());
        t.start();

        Thread.sleep(500);
        System.out.println("线程状态3:"+t.getState());
    }
}

在这里插入图片描述
线程优先级

/*线程优先级*/
public class ThreadDempByPriority
{
    public  static void main(String[] args)
    {
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                Thread t=Thread.currentThread();
                System.out.println("线程优先级:"+t.getPriority());
            }
        });
        System.out.println("线程优先级2"+thread.getPriority());
        thread.start();
        Thread.sleep(500);
        System.out.println("线程优先级3:"+ thread.getPriority());
    }

}

/*线程优先级*/
public class ThreadDempByPriority
{
    public  static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                Thread t=Thread.currentThread();
                System.out.println("线程优先级:"+t.getPriority());
            }
        });
        System.out.println("线程优先级2"+thread.getPriority());
        thread.start();
        Thread.sleep(500);
        System.out.println("线程优先级3:"+ thread.getPriority());
    }

}

在这里插入图片描述

public class ThreadDemo13 {
    public static void main(String[] args) throws InterruptedException {
   Runnable runnable=new Runnable() {
    @Override
    public void run() {
            Thread t=Thread.currentThread();
            System.out.println("线程ID:"+t.getId());
            System.out.println("线程名称:"+t.getName());
        System.out.println("线程优先级"+t.getPriority());
    }
};
        Thread thread=new Thread(runnable,"线程1");
        thread.start();

       Thread.sleep(500);
        System.out.println();

        Thread thread2=new Thread(runnable,"线程2");
        thread2.start();

        Thread thread3=new Thread(runnable,"线程3");
                thread3.start();
    }
}

在这里插入图片描述
每个线程保证代码从上往下执行
优先级讨论 执行顺序 多线程

import java.awt.*;

public class ThreadByPriority2 {
    private final static int MAXCOUNT=100;
    public static void main(String[] args) {
        Thread t1=new Thread(new Runnable() {
            @Override
            public void run() {
                //得到当前现线程
                Thread t=Thread.currentThread();
                int priority=t.getPriority();
                for (int i = 0; i < 1000; i++) {
                    System.out.println(t.getName()+"-优先级:"+priority);
                }

            }
        });//设置优先就 10
        t1.setPriority(Thread.MAX_PRIORITY);
    Thread t2=new Thread(new Runnable() {
        @Override
        public void run() {
            Thread t=Thread.currentThread();
            int priority=t.getPriority();
            for (int i = 0; i < MAXCOUNT; i++) {
                System.out.println(t.getName()+"-优先级"+priority);
            }
        }
    },"线程2");
    t2.setPriority(Thread.MIN_PRIORITY);

    Thread t3=new Thread(new Runnable() {
        @Override
        public void run() {
            Thread t=Thread.currentThread();
            int priority=t.getPriority();
            for (int i = 0; i <MAXCOUNT ; i++) {
                System.out.println(t.getName()+"-优先级:"+priority);
            }
        }
    },"线程3");
    t3.setPriority(Thread.NORM_PRIORITY);
    t2.start();
    t1.start();
    t3.start();
}
}

高优先级被执行概率更大

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述高概率高优先级先执行完

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值