一、前言
二、什么是线程
线程,一般指的是进程中执行任务的流程。每个进程开启后都会拥有独立的内存空间,一个进程可以执行多个线程,而多个线程共享一个进程的内存空间。线程只能是进程的一部分,它能够满足我们提高内存的使用效率。
三、线程的生命周期
线程是一个动态执行的过程,它有自己的生命周期。线程的五种状态:新建(New):java.lang.Thread或其子类创建一个新的线程,此时该线程处于新建状态,该线程一直在等待start()方法调用。就绪(Runnable):当调用该线程对象的start()方法后,此时线程处于就绪状态,该线程并没有立即执行而是一直在等待CUP的调度。运行(Running):当就绪状态的线程获取到CPU的资源后,会执行run()方法内容,此时线程处于运行状态,该线程只有处于就绪状态才能进入运行状态。阻塞(Blocked):如果一个线程执行了睡眠(sleep)、等待(wait)等方法,失去所占用资源之后,该线程就从运行状态进入阻塞状态。在睡眠时间已到或获得设备资源后可以重新进入就绪状态。可以分为三种:
等待阻塞:运行状态中的线程执行 wait() 方法,使线程进入到等待阻塞状态。
同步阻塞:线程在获取 synchronized 同步锁失败(因为同步锁被其他线程占用)。其他阻塞:通过调用线程的 sleep() 或 join() 发出了 I/O 请求时,线程就会进入到阻塞状态。当sleep() 状态超时,join() 等待线程终止或超时,或者 I/O 处理完毕,线程重新转入就绪状态。死亡(Dead):线程执行完毕或者因异常退出,该线程结束生命周期。线程常用的调度方法:
start(): 启动,开始执行该线程任务。
run():运行,JVM调用线程任务。wait():等待,使线程进入阻塞列队。sleep():休眠,使正在执行线程进入睡眠状态。join():联合,把指定的线程加入到正在执行的线程,合并该线程执行任务。yield():让步,暂停该线程运行,并执行其他线程。notify():唤醒,使阻塞列队的线程进入就绪状态,并等待执行。notifyAll():唤醒,使所有的阻塞列队的线程进入就绪状态,并等待执行。interrupt():中断,让线程暂时的放弃执行任务。四、线程的优先级线程的执行顺序通常与优先级相关,越高级别的优先级获得优先执行几率更大。线程的优先级是一个整数,其取值范围是1-10,数字越大优先级越高。设置线程优先级:public final int setPriority(int priority)线程常用的优先级:
Thread.MIN_PRIORITY:最小优先级,数值为1
Thread.NORM_PRIORITY:默认优先级,数值为5Thread.MAX_PRIORITY:最大优先级,默认为10五、实现线程的方式5.1 继承Thread类
public class ThreadDemo extends Thread{
private String threadName;ThreadDemo(String threadName){ this.threadName = threadName;} @Overridepublic void run() { for (int i = 0; i < 10; i++) { System.out.println("Thread: " + threadName + ", " + i); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } }}
}public static void main(String[] args) {
ThreadDemo demo1 = new ThreadDemo("Thread-1");demo1.start(); // 启动线程1 ThreadDemo demo2 = new ThreadDemo("Thread-2");demo2.start(); // 启动线程2}
运行结果:
Thread: Thread-2, 0Thread: Thread-1, 0Thread: Thread-2, 1Thread: Thread-1, 1Thread: Thread-2, 2Thread: Thread-1, 2Thread: Thread-1, 3Thread: Thread-2, 3Thread: Thread-2, 4Thread: Thread-1, 4Thread: Thread-2, 5Thread: Thread-1, 5Thread: Thread-1, 6Thread: Thread-2, 6Thread: Thread-1, 7Thread: Thread-2, 7Thread: Thread-1, 8Thread: Thread-2, 8Thread: Thread-2, 9Thread: Thread-1, 95.2 实现Runnable接口public class RunnableDemo implements Runnable{
private String threadName;RunnableDemo(String threadName){ this.threadName = threadName;} @Overridepublic void run() { for (int i = 0; i < 10; i++) { System.out.println("Thread: " + threadName + ", " + i); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } }}
}public static void main(String[] args) {
RunnableDemo demo1 = new RunnableDemo("Thread-1"); new Thread(demo1).start(); // 启动线程1RunnableDemo demo2 = new RunnableDemo("Thread-2"); new Thread(demo2).start(); // 启动线程2}
输出结果:
Thread: Thread-1, 0Thread: Thread-2, 0Thread: Thread-1, 1Thread: Thread-2, 1Thread: Thread-2, 2Thread: Thread-1, 2Thread: Thread-1, 3Thread: Thread-2, 3Thread: Thread-2, 4Thread: Thread-1, 4Thread: Thread-2, 5Thread: Thread-1, 5Thread: Thread-1, 6Thread: Thread-2, 6Thread: Thread-2, 7Thread: Thread-1, 7Thread: Thread-1, 8Thread: Thread-2, 8Thread: Thread-2, 9Thread: Thread-1, 95.3 实现Callable接口public class CallableDemo implements Callable<Integer>{ @Override
public Integer call() throws Exception { int result = 0; for (int i = 0; i < 10; i++) { result = i; System.out.println(Thread.currentThread().getName()+" "+ i ); } return result;}
}public static void main(String[] args) {
ExecutorService executor = Executors.newSingleThreadScheduledExecutor();CallableDemo callableDemo = new CallableDemo();Futureresult = executor.submit(callableDemo);executor.shutdown(); try { Thread.sleep(1000);} catch (InterruptedException e1) { e1.printStackTrace();} try { System.out.println("运行结果:"+ result.get());} catch (InterruptedException e) { e.printStackTrace();} catch (ExecutionException e) { e.printStackTrace();}
}
运行结果:
pool-1-thread-1 0pool-1-thread-1 1pool-1-thread-1 2pool-1-thread-1 3pool-1-thread-1 4pool-1-thread-1 5pool-1-thread-1 6pool-1-thread-1 7pool-1-thread-1 8pool-1-thread-1 9运行结果:9