多线程
创建进程方式:
(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()方法正在运行");
}
}
}
(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()方法正在运行");
}
}
}
使用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;
}
}
应用:售票场景,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--+"张票");
}
}
}
用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--+"张票");
}
}
}