线程Thread(1)

一 线程简介
应用程序以进程为单位运行,一个进程之内可以分为一到多个线程

window下可以通过任务管理器查看进程

linux 下可以通过ps -fe

进程、线程都可以并行执行, cpu

---程序1
              ---程序2
                             ---程序1

操作系统中有一个组件叫做任务调度器,将cpu的时间片分给不同的程序使用, 微观串行(单核),宏观并行.(多核cpu可以并行)

好处:
1) 多进程,多线程可以让程序不被阻塞.
2) 充分利用多核cpu的优势,提高运行效率
二.如何创建线程
创建线程的两种方法:
(1)通过实现Thread的子实现类(通过继承),然后重写run()方法

public class TestThread2 {
    public static void main(String[] args) {
        Thread t = new MyThread();
        t.start();
    }
}

class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("线程1");
    }
}

(2)通过实现runnable接口,然后重写run()方法

public class Test2 {
    public static void main(String[] args) {
        Runnable r=new Runnable() {
            @Override
            public void run() {
                System.out.println("线程2");
            }
        };
        Thread t=new Thread(r);
        t.start();
    }

}

三:线程中常用的方法
Thread.sleep(long n); // 让当前线程休眠n毫秒
Thread.currentThread(); // 找到当前线程

    public class Test2 {
    public static void main(String[] args) {
     Thread t1=new Thread(){
       public void run(){
           try {
               sleep(2000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           System.out.println(Thread.currentThread()+"线程1");
       }
     };
        Thread t2=new Thread(){
            public void run(){
                System.out.println("线程2");
            }
        };
        t1.start();

        t2.start();
    }


}

输出结果:

线程2
Thread[Thread-0,5,main]线程1

join() 等待某个线程执行结束

public class Test2 {
    public static void main(String[] args) throws InterruptedException {
     Thread t1=new Thread(){
       public void run(){
           try {
               sleep(2000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           System.out.println(Thread.currentThread()+"线程1");
       }
     };
        Thread t2=new Thread(){
            public void run(){
                System.out.println("线程2");
            }
        };
        t1.start();
        t1.join();
        t2.start();
    }
}

运行结果

Thread[Thread-0,5,main]线程1
线程2

main 方法实际是由主线程(main)来调用的
实例方法:
start() 让线程启动, 只能调用一次,如果调用了多次会出现IllegalThreadStateException

直接调用run和使用start间接调用run的区别:
直接调用run是在主线程中执行了run,没有启动新的线程
使用start是启动新的线程,通过新的线程间接执行run

public class SY {
    public static void main(String[] args) {
        Object obj=new Object();
        Thread t1=new Thread(){
          public void run(){
              synchronized (obj){
                  System.out.println("线程1开始运行");
                  try {
                      obj.wait();
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  System.out.println("线程1运行中");
              }
          }
        };
        Thread t2=new Thread(){
            public void run(){
                synchronized (obj){
                    System.out.println("线程2开始运行");
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程2运行中");
                }
            }
        };
        //t1.start();
        t1.run();
        //t2.start();
    }

}
运行结果
线程1开始运行
public class SY {
    public static void main(String[] args) {
        Object obj=new Object();
        Thread t1=new Thread(){
            public void run(){
                synchronized (obj){
                    System.out.println("线程1开始运行");
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程1运行中");
                }
            }
        };
        Thread t2=new Thread(){
            public void run(){
                synchronized (obj){
                    System.out.println("线程2开始运行");
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程2运行中");
                }
            }
        };
        t1.start();
        //t1.run();
        t2.start();
    }

}

运行结果

线程1开始运行
线程2开始运行

由以上的例子可以看出,如果直接调用run()则执行流程为:执行主线程,由主线程调用run()方法,执行了第一个run()方法的第一话后由于wait语句之后的代码未执行,而调用start()则是创建了俩个新线程,俩个新线程并行执行run()方法的第一句.所以start()方法实现了创建新线程,真正实现了多线程运行.


obj.wait(); 让object监视器的线程等待
obj.notify(); 让object上正在等待的线程中挑一个唤醒
obj.notifyAll(); 让object上正在等待的线程全部唤醒

public class SY {
    public static void main(String[] args) throws InterruptedException {
        Object obj=new Object();
        Thread t1=new Thread(){
            public void run(){
                synchronized (obj){
                    System.out.println("线程1开始运行");
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程1运行中");
                }
            }
        };
        Thread t2=new Thread(){
            public void run(){
                synchronized (obj){
                    System.out.println("线程2开始运行");
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程2运行中");
                }
            }
        };
        t1.start();
        t2.start();
        Thread.sleep(2000);
        synchronized (obj){
            obj.notifyAll();
        }
    }

}

代码末尾加了句Thread.sleep(2000);是为了确保俩个线程可以执行wait语句,然后执行唤醒语句,如果不加这句代码,主线程会优先执行唤醒语句(notifyAll)导致一次空唤醒,之后再执行wait语句此时剩下的代码将无法唤醒
四:守护线程

有一种特殊的线程叫做守护(守护的是主线程)线程,只要主线程运行结束,即使守护线程的代码没有执行完,也会跟着主线程一起结束,例:
```java
Thread t1= new Thread(()->{
    System.out.println("守护线程开始执行...");
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("守护线程结束");
});
t1.setDaemon(true); // 设置该线程为守护线程
t1.start();

Thread.sleep(1000);




<div class="se-preview-section-delimiter"></div>

interrupt() 可以打断正在等待的线程(包括sleep, join的等待)例如:

Thread t = new Thread(()->{
    try {
        Thread.sleep(5000); // 被打断线程会抛出InterruptedException
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("结束 。。。");
});
t.start();

Thread.sleep(1000);
System.out.println("打断t线程。。。");
t.interrupt();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值