Java之多线程的实现与应用

多线程

创建进程方式:

(1)继承Thread类

class Main {
    public static void main(String[] args) {
MyThread01 myThread01=new MyThread01();
myThread01.start();
while(true){
    System.out.println("main方法的run()方法正在运行");
}
    }
}
​class MyThread01 extends Thread {
    public void run(){
        while(true){
            System.out.println("MyThread01类的run()方法正在运行");
        }
    }
​
}

 

 

0d35a5f27bac60fadb3ba1b8bfc17c56.png

(2)实现Runnable接口

为一个实现了Runnable接口的类创建一个实例化对象。该对象作为参数用来创建thread对象,然后由该对象启动start方法开启新线程。主线程继续执行main()方法。

class Main {
 public static void main(String[] args) {
MyThread01 myThread01=new MyThread01();
Thread thread=new Thread(myThread01);
thread.start();
while(true){
 System.out.println("main方法的run()方法正在运行");
}
 }
}
​class MyThread01 implements Runnable{    public void run(){
        while(true){
            System.out.println("MyThread01类的run()方法正在运行");
        }
    }
​
}
 

 

9f66254923c23cacee6bd5b87b7fdb71.png

使用Lambda表达式实现如下:

使用( )->{  }代替作为实现了接口的类的实例化对象作为参数传入;

class Main {
    public static void main(String[] args) {
​
        Thread thread=new Thread(()->{
            while(true){
                System.out.println("main方法的run()方法正在运行");
            }
        });
        thread.start();
​
    }
}
​
class MyThread01 implements Runnable{
    public void run(){
        while(true){
            System.out.println("MyThread01类的run()方法正在运行");
        }
    }
​
}

(3)实现Callable接口,与实现Runnable接口相比,可以获得返回值。

首先创建一个实现了Callable接口的类MyThread的实例化对象,在该类中重写call()方法返回一个返回值。

然后用FutureTask封装MyThread类的对象,调用有参的Thread方法创建线程对象,由该对象调用start方法启动线程,然后从FutureTask返回返回值。主线程执行main()方法。

FutureTask类的直接接口是RunnableFuture.该接口拓展出Runnable和Future两个接口。

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
​
class Main {
    public static void main(String[] args) throws InterruptedException,
            ExecutionException {
MyThread01 myThread01=new MyThread01();
        FutureTask<Object> ft=new FutureTask<>(myThread01);
        Thread thread=new Thread(ft);
        thread.start();
        System.out.println(Thread.currentThread().getName()+"的返回结果是"+ft.get());
        int a=3;
        while(a++<7){
            System.out.println(Thread.currentThread().getName()+"的main()方法在运行");
        }
    }
}
​
class MyThread01 implements Callable<Object> {
   public Object call() throws Exception {
       int i=0;
       while(i++<5){
           System.out.println(Thread.currentThread().getName()+"的call()方法正在运行");
       }
​
       return  i;
   }
​
}

 

7efd65896d21e581366a332582279170.png

 

2cfb8e54682df6891403dee5fdcc158e.png

 

应用:售票场景,4个窗口同时发售次日的100张火车票,模拟售票场景

继承Thread类实现售票场景:

 
class Main {
    public static void main(String[] args) {
        new TicketWindow().start();
        new TicketWindow().start();
        new TicketWindow().start();
        new TicketWindow().start();
    }
}
class TicketWindow extends Thread{
    private int tickets=2;
    public void run(){
        while(tickets>0){
            Thread th=Thread.currentThread();
            String name = th.getName();
            System.out.println(name+"在发行第"+tickets--+"张票");
        }
    }
}

 

71c4f061011339f0ba8e2f9952d1d4b6.png

用Thread创建多线程,无法保证正确操作,接下来通过实现Runnable接口方式实现,使用Thread*(Runnable target,String name)方法在创建线程对象同时指定线程名称。

class Main {
    public static void main(String[] args) {
        TicketWindow tw=new TicketWindow();
        new Thread(tw,"窗口1").start();
        new Thread(tw,"窗口2").start();
        new Thread(tw,"窗口3").start();
        new Thread(tw,"窗口4").start();
​
    }
}
class TicketWindow implements Runnable{
​
    private int tickets=100;
    public void run() {
        while(tickets>0){
            Thread thread=Thread.currentThread();
            String name = thread.getName();
            System.out.println(name+"在发行第"+tickets--+"张票");
​
        }
    }
​
​
}
 

 

070f14e201c919ef9f76e5782b75e191.png

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值