多线程
Java中有单线程和多线程。单线程即单个任务,一个执行路径,一个顺序流;多线程即多个任务,多个执行流程,多个顺序流。
三高网站:高性能,高可用,高并发
多线程的优点:资源利用率更好,程序设计在某些情况下更简单,程序响应更快
1. 线程的创建和开启方法
1.1 继承Thread类
继承Thread类,重写run()方法,方法内部定义线程体,使用start()方法开启线程。
代码:
//定义类继承Thread类
public class ThreadDemo01 extends Thread {
public static void main(String[] args) {
//创建线程
ThreadDemo01 th = new ThreadDemo01();
//开启线程
th.start();
for (int i=1;i<=20;i++){
System.out.println("一边喝水!");
try {
//间隔10ms
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//重写run()方法
@Override
public void run() {
for (int i=1;i<=20;i++){
System.out.println("一边学习!");
try {
//间隔10ms
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
1.2 实现Runnable接口
实现Runnable接口,重写run()方法,方法内部定义线程体,使用start()方法开启线程。
代码:
//定义类实现Runnable接口
public class ThreadDemo02 implements Runnable {
public static void main(String[] args) {
//创建线程
ThreadDemo02 th = new ThreadDemo02();
Thread thread = new Thread(th);
//开启线程
thread.start();
for (int i=0;i<=20;i++){
System.out.println("一边喝水!");
try {
//间隔10ms
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//重写run()方法
@Override
public void run() {
for (int i=0;i<=20;i++){
System.out.println("一边吃饭!");
try {
//间隔10ms
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
练习1:
//使用多线程模拟3个人买100张票
public class Web12306Demo implements Runnable {
int tickets = 100;
//重写run()方法
@Override
public void run() {
while (true){
if (tickets<=0){
break;
}
//打印谁买了第几张票,同时票数量-1
System.out.println(Thread.currentThread().getName()+"抢到了第"+ tickets-- +"张票");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//主方法
public static void main(String[] args) {
//创建线程和开启线程
Web12306Demo web = new Web12306Demo();
new Thread(web,"张三").start();
new Thread(web,"李四").start();
new Thread(web,"王五").start();
}
}
练习2:
//龟兔赛跑 参赛者跑满一百步结束 乌龟:一直跑不休息 兔子:每20步休息2ms如果一旦有人赢了游戏就结束
public class ThreadDemo05 {
public static void main(String[] args) {
//创建线程,开启线程
Match match = new Match();
new Thread(match,"兔子").start();
new Thread(match,"乌龟").start();
}
}
//定义比赛类实现Runnable接口
class Match implements Runnable {
//定义结束的标签,有胜利者了比赛结束
private String winner;
//重写run()方法
@Override
public void run() {
//i作为步数
for (int i=1;i<=100;i++){
System.out.println(Thread.currentThread().getName()+"跑了"+i+"步");
//判断如果是兔子且跑到20的倍数步时休息2ms
if ("兔子".equals(Thread.currentThread().getName()) && i%20==0){
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//判断比赛是否结束,结束即结束循环
if (checkOver(i)){
break;
}
}
}
//定义方法检查比赛是否结束 true:比赛结束 false:比赛未结束
public boolean checkOver(int steps){
//有人赢了
if (winner!=null){
return true;
}
//自己赢了
if (steps==100){
winner = Thread.currentThread().getName();
System.out.println(winner+"赢了!!");
return true;
}
return false;
}
}
1.3 实现Callable接口
实现Callable接口,重写call()方法,方法内部定义线程体,submit()方法提交任务开启线程。
这种创建开启线程的方法的优点是call()方法可以抛出异常,也可以定义返回值。
代码(练习2龟兔赛跑修改):
public class ThreadDemo06 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
Match match = new Match();
//通过线程池 固定大小线程池2个 static ExecutorService newFixedThreadPool(int nThreads)
//1.创建执行服务
ExecutorService service = Executors.newFixedThreadPool(2);
//2.提交任务 <T> Future<T> submit(Callable<T> task) 提交值返回任务以执行并返回表示任务的挂起结果的Future
Future<Integer> f1 = service.submit(match);
Future<Integer> f2 = service.submit(match);
//3.获取结果
Integer result1 = f1.get();
Integer result2 = f2.get();
System.out.println(result1);
System.out.println(result2);
//4.结束服务
service.shutdown();
}
}
class Match implements Callable<Integer> {
private String winner;
@Override
public Integer call() throws Exception {
for (int i=1;i<=100;i++){
System.out.println(Thread.currentThread().getName()+"跑了"+i+"步");
//线程池中线程名字为pool-1-thread-1/2,假设thread-2为兔子
if ("pool-1-thread-2".equals(Thread.currentThread().getName()) && i%20==0){
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (checkOver(i)){
return i;
}
}
return null;
}
public boolean checkOver(int steps){
//有人赢了
if (winner!=null){
return true;
}
//自己赢了
if (steps==100){
winner = Thread.currentThread().getName();
System.out.println(winner+"赢了!!");
return true;
}
return false;
}
}