Java学习26 2020-03-12

内容

1.创建线程的另外一种方法以及Thread中几个方法介绍
2.线程的优先级
3.sleep方法

一.创建线程的另外一种方法以及Thread中几个方法介绍

1.另外一种方法?

实现Runnable接口
这个时候创建线程的代码就要这样写Thread t1 = new Thread(new Processor());
而上一个方法直接是Thread t1 = new Processor()

2.Thread中的几个常用方法

①获取当前线程引用
②获取当前线程的名字
③更改线程的名字

3.使用示例
public class 测试程序   {                                                                                                             
                                                                                                                        
    public static void main(String[] args)  {
   
        //1.获取当前线程对象
        Thread t = Thread.currentThread();//t保存的内存地址指向的线程是“主线程对象”
        
        //2.获取线程的名字
        System.out.println(t.getName());//输出main
        
        //3.创建线程
        Thread t1 = new Thread(new Processor());//如果线程使用的继承,那么可以直接写Thread t1 = new Processor();注意两种区别
        
        //4.获取线程的名字
        System.out.println(t1.getName());
        
        //5.给线程起名
        t1.setName("t1");
        
        t1.start();
        
        
    }

   
    
}

//使用实现Runnable接口的方式创建线程
class Processor implements Runnable
{
    public void run() {
        Thread t = Thread.currentThread();//t保存的内存地址指向的线程是“t1线程对象”
        
        System.out.println(t.getName());
    }
}

输出结果:

main
Thread-0
t1

二.线程的优先级

1.优先级高低有什么影响?

优先级高的线程获取的CPU时间片相对多一些

2.优先级范围

最低1
最高10
默认5

3.关于优先级的一些方法

①得到优先级
②设置优先级

4.使用示例
public class 测试程序   {                                                                                                             
                                                                                                                        
    public static void main(String[] args)  {
   
        //1.打印几个数据
        System.out.println(Thread.MAX_PRIORITY);//10
        System.out.println(Thread.MIN_PRIORITY);//1
        System.out.println(Thread.NORM_PRIORITY);//5
        
        //2.创建线程
        Thread t1 = new Processor();
        t1.setName("t1");
        Thread t2 = new Processor();        
        t2.setName("t2");
        
        //3.打印线程的优先级
        System.out.println(t1.getPriority());
        System.out.println(t2.getPriority());
        
        //4.设置优先级
        t1.setPriority(1);
        t2.setPriority(10);
        
        //5.启动线程
        t1.start();
        t2.start();
                
    }

   
    
}


class Processor extends Thread
{
    public void run() {
        Thread t = Thread.currentThread();
        System.out.println(t.getName());
    }
}

输出结果
10
1
5
5
5
t2
t1或者是
10
1
5
5
5
t1
t2

三.sleep方法

1.sleep方法简介

sleep方法是一个静态方法(最好使用 类名点 的方式调用)

2.使用形式

Thread.sleep(毫秒);

3.方法作用:

阻塞当前线程,腾出CPU,让给其他线程。

4.使用示例
public class 测试程序   {                                                                                                             
                                                                                                                        
    public static void main(String[] args)  {
   
        Thread t1 = new Processor();
        t1.setName("t1");
        t1.start();
        
        
                
    }

   
    
}


class Processor extends Thread
{
    //Thread中的run方法不抛出异常,所以重写run方法之后,在run方法的生命位置上不能使用throws
    //所以run方法中的异常只能try...catch...
    public void run() {
        
        for(int i = 0;i < 10;i++) {
            System.out.println(Thread.currentThread().getName() + "--->" + i);
            
            //使用trycatch
            try {
                Thread.sleep(1000);//阻塞1s钟
            }catch(InterruptedException e) {
                e.printStackTrace();
            }
            
            //m1();
        }
    }
    
    //m1方法可以使用throws
    /*public void m1() throws Exception{
        
    }*/
}

结果:
t1--->0
t1--->1
t1--->2
t1--->3
t1--->4
t1--->5
t1--->6
t1--->7
t1--->8
t1--->9每一秒输出一行

5.面试题

问:t.sleep(1000)是否能使t线程休眠1s?

public class 测试程序   {                                                                                                             
                                                                                                                        
    public static void main(String[] args) throws InterruptedException  {
   
        //1.创建线程
        Thread t = new Processor();
        t.setName("t");
        
        //2.启动线程
        t.start();
        
        //3.休眠
        t.sleep(1000);
        //由于sleep是静态方法,所以等同于Thread.sleep(1000)
        //也就是说阻塞的是当前线程,和t线程无关
        
        System.out.println("helloworld!");
        //这个helloworld会在main阻塞1s后输出
                
    }

   
    
}


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

}
6.打断sleep
public class 测试程序   {                                                                                                             
                                                                                                                        
    public static void main(String[] args) throws InterruptedException  {
   
        //需求:启动线程,5s之后打断线程的休眠
        
        //1.创建线程
        Thread t = new Thread(new Processor());
        
        //2.起名
        t.setName("t");
        
        //3.启动线程
        t.start();
        
        //4.5s之后
        Thread.sleep(5000);
        
        //5.打断t的休眠
        t.interrupt();
   
                
    }

   
    
}


class Processor implements Runnable
{
    
    public void run() {
       try {
           Thread.sleep(10000000L);//打断之后就发生异常
           //也就是说这个interrupt是靠的异常处理机制来实现的
           
           System.out.println("Helloworld!");//这句话不会执行
       }catch(InterruptedException e) {
           e.printStackTrace();
       }
       
       for(int i = 0;i < 5;i++) {
           System.out.println(i);
       }
    }
    

}

输出结果

java.lang.InterruptedException: sleep interrupted
0
1
2
3
4
    at java.lang.Thread.sleep(Native Method)
    at 对象.Processor.run(测试程序.java:40)
    at java.lang.Thread.run(Unknown Source)

如果是把e.printStackTrace();注释掉

catch(InterruptedException e) {
           //e.printStackTrace();
       }

则输出

0
1
2
3
4
7.正确的终止程序

刚才的interrupt说靠的异常处理机制,更推荐使用下面这种方式。

public class 测试程序   {                                                                                                             
                                                                                                                        
    public static void main(String[] args) throws InterruptedException  {
   
        Processor p = new Processor();
        Thread t = new Thread(p);
        
        t.setName("t");
        
        t.start();
        
        //5s之后终止
        Thread.sleep(5000);
        
        //终止
        p.run = false;
   
                
    }

   
    
}


class Processor implements Runnable
{
    
    boolean run = true;
    
    public void run() {
      for(int i = 0;i < 10;i++) {
          if(run) {
              
              try {
                  Thread.sleep(1000);
              }catch(Exception e) {
                  e.printStackTrace();
              }
              
              System.out.println(i);
          }else {
              return;
          }
      }
    }
    

}

输出结果
0
1
2
3
4之后因为已经终止,所以剩下的数字没有输出

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值