一、什么是多线程
线程(Thread):是程序中一个单一的顺序控制流程.在单个程序中同时运行多个线程完成不同的工作,称为多线程.
多线程(multithreading):是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。具有这种能力的系统包括对称多处理机、多核心处理器以及芯片级多处理(Chip-level multithreading)或同时多线程(Simultaneous multithreading)处理器。在一个程序中,这些独立运行的程序片段叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理(Multithreading)”。
二、线程的创建
Java中启用多线程有两种方式:①继承Thread类;②实现Runnable接口。
1. 继承Thread类,并覆写接口中的run()方法
/**
* @describe MyThread继承Thread类
* @author Li DongWei
*/
class MyThread extends Thread{
/*
* 覆写run()方法,定义该线程需要执行的代码
*/
@Override
public void run() {
}
}
线程创建好了,该到启动线程了。创建一个该类的实例,并调用start()方法,将开启一个线程,并执行线程类中覆写的run()方法。
public class MThread {
public static void main(String[] args) {
Thread t1 = new MyThread();
Thread t2 = new MyThread();
t1.start();
t2.start();
}
}
2. 实现Runnable接口,并覆写接口中的run()方法,这是推荐的也是最常用的方式。Runnable接口定义非常简单,就只有一个抽象的run()方法
/**
* @describe MyThread继承Thread类
* @author Li DongWei
*/
class MyThread implements Runnable{
@Override
public void run() {
}
}
这时候线程创建好了,我们会发现这种方式没有start()方法,我们又该怎么启动呢?
我们需要创建一个MyThread类的实例,作为Thread类的构造函数的参数传入,并通过该Thread类的实例来调用start()方法从而开启线程。
public class MRunnable {
public static void main(String[] args) {
MyThread mThread = new MyThread();
Thread t1 = new Thread(mThread);
Thread t2 = new Thread(mThread);
t1.start();
t2.start();
}
}
两种方式都能创建好线程并启动,那么这两种有什么不同呢?
这儿假设一个场景,某地火车站剩余5张火车票,然后有3个窗口在同时卖票。用代码分别实现如下:
方式一:
/**
* @describe 继承Thread类,实现卖票操作
* @author Li DongWei
*/
class MyThread extends Thread{
private int CountTicked = 5;
private String name;
public MyThread(String name) {
this.name = name;
}
//重构run()方法
@Override
public void run() {
while(CountTicked > 0)
{
CountTicked--;
System.out.println(name + "卖票," + "剩余" + CountTicked);
}
}
}
public class TickedThread {
public static void main(String[] args) {
//创建线程实例,调用含有一个参数的构造函数
Thread t1 = new MyThread("窗口1");
Thread t2 = new MyThread("窗口2");
Thread t3 = new MyThread("窗口3");
//启动线程
t1.start();
t2.start();
t3.start();
}
}
控制台打印
窗口1卖票,剩余4
窗口1卖票,剩余3
窗口1卖票,剩余2
窗口1卖票,剩余1
窗口1卖票,剩余0
窗口3卖票,剩余4
窗口3卖票,剩余3
窗口3卖票,剩余2
窗口3卖票,剩余1
窗口3卖票,剩余0
窗口2卖票,剩余4
窗口2卖票,剩余3
窗口2卖票,剩余2
窗口2卖票,剩余1
窗口2卖票,剩余0
方式二:
/**
* @describe 实现Runnable接口,实现卖票操作
* @author Li DongWei
*/
class MyThread implements Runnable{
private int CountTicked = 5;
@Override
public void run() {
while(CountTicked > 0)
{
CountTicked--;
System.out.println(Thread.currentThread().getName() + "卖票," + "剩余" + CountTicked);
}
}
}
public class TickedRunNable {
public static void main(String[] args) {
MyThread mThread = new MyThread();
Thread t1 = new Thread(mThread, "窗口1");
Thread t2 = new Thread(mThread, "窗口2");
Thread t3 = new Thread(mThread, "窗口3");
//启动线程
t1.start();
t2.start();
t3.start();
}
}
控制台打印
窗口1卖票,剩余4
窗口1卖票,剩余1
窗口1卖票,剩余0
窗口3卖票,剩余2
窗口2卖票,剩余3
对比很容易发现,方式一一共卖了15张票,这跟前面定义的火车站只有5张票明显对不上,然而方式二就刚好卖了5张票。这是为什么呢,其实奥妙就在于方式一直接new了三个新线程,三个新线程各自拥有各自的资源对象。然而方式二在创建的时候,只new了一个runnable对象,并且新建的三个线程用到同一个Runnable对象作为参数,等于在运行的时候,这三个线程用到的是同一个Runnable资源。
两种方式的比较:
继承Thread类的方式有它固有的弊端,因为Java中继承的单一性,继承了Thread类就不能继承其他类了;同时也不符合继承的语义,Dog跟Thread没有直接的父子关系,继承Thread只是为了能拥有一些功能特性。而实现Runnable接口,①避免了单一继承的局限性,②同时更符合面向对象的编程方式,即将线程对象进行单独的封装,③而且实现接口的方式降低了线程对象(Dog)和线程任务(run方法中的代码)的耦合性,④如上面所述,可以使用同一个Dog类的实例来创建并开启多个线程,非常方便的实现资源的共享。实际上Thread类也是实现了Runnable接口。实际开发中多是使用实现Runnable接口的方式。
部分内容参考:Java基础-多线程-①线程的创建和启动