版权声明:本文为博主原创文章,无需授权即可转载,甚至无需保留以上版权声明,转载时请务必注明作者。
https://blog.csdn.net/weixin_43453386/article/details/88236004
java——》实现多线程
多进程作用:
不是提高执行速度,而是提高 CPU 的使用率。
进程和进程之间的内存是独立的。
多线程作用:
不是提高执行速度,而是提高应用程序的使用率。
线程和线程共享“堆内存和方法区内存”,栈内存是独立的,一个线程一个栈。
给现实世界中的人类一种错觉:感觉多个线程在同时并发执行。
一、继承Thread类
1、步骤
- 1)继承Thread类,重写该类的Run方法(代表了该线程需要完成的任务)
- 2)创建Thread类的实例(即创建了线程对象)
- 3)调用线程的start方法启动线程
2、示例代码
public class ExtendThread {
public static void main(String[] args) {
MyThread1 m1 = new MyThread1("A");
MyThread1 m2 = new MyThread1("B");
m1.start();
m2.start();
}
}
class MyThread1 extends Thread {
private String name;
public MyThread1(String name) {
this.name = name;
}
public void run() {
for (int x = 0; x < 10; x++) {
System.out.println(this.name + "x=" + x+ Thread.currentThread().getName());
}
}
}
3、运行结果
Connected to the target VM, address: '127.0.0.1:50931', transport: 'socket'
Bx=0Thread-1
Bx=1Thread-1
Bx=2Thread-1
Bx=3Thread-1
Bx=4Thread-1
Bx=5Thread-1
Bx=6Thread-1
Bx=7Thread-1
Bx=8Thread-1
Bx=9Thread-1
Ax=0Thread-0
Ax=1Thread-0
Ax=2Thread-0
Ax=3Thread-0
Ax=4Thread-0
Ax=5Thread-0
Ax=6Thread-0
Ax=7Thread-0
Ax=8Thread-0
Ax=9Thread-0
Disconnected from the target VM, address: '127.0.0.1:50931', transport: 'socket'
Process finished with exit code 0
二、实现Runnable接口
1、步骤
- 1)定义实现Runnable接口的类,重写该类的Run方法(代表了该线程需要完成的任务)
- 2)创建Runnable实现类的实例,并将此实例作为Thread的target创建一个Thread对象(即创建了线程对象)
- 3)调用线程的start方法启动线程
2、示例代码
public class ImplRunnable {
public static void main(String[] args) {
MyThread2 m1 = new MyThread2("A");
Thread t1 = new Thread(m1);
Thread t2 = new Thread(m1);
t1.start();
t2.start();
}
}
class MyThread2 implements Runnable {
private String name;
public MyThread2(String name) {
this.name = name;
}
@Override
public void run() {
for (int x = 0; x < 10; x++) {
System.out.println(this.name + "x=" + x + Thread.currentThread().getName());
}
}
}
3、运行结果
Connected to the target VM, address: '127.0.0.1:51381', transport: 'socket'
Ax=0Thread-0
Ax=1Thread-0
Ax=2Thread-0
Ax=3Thread-0
Ax=4Thread-0
Ax=5Thread-0
Ax=6Thread-0
Ax=7Thread-0
Ax=8Thread-0
Ax=9Thread-0
Ax=0Thread-1
Ax=1Thread-1
Ax=2Thread-1
Ax=3Thread-1
Ax=4Thread-1
Ax=5Thread-1
Ax=6Thread-1
Ax=7Thread-1
Ax=8Thread-1
Ax=9Thread-1
Disconnected from the target VM, address: '127.0.0.1:51381', transport: 'socket'
Process finished with exit code 0
4、总结
采用Ruunable接口的方式创建多个线程,可以共享线程类的实例变量
- 程序创建的Runnable对象只是线程的target
- 多个线程可以共享一个target
三、实现Callable接口
1、步骤
- 1)创建Callable接口的实现类,重写该类的call方法(代表了该线程需要完成的任务,且有返回值)
- 2)使用FutrueTask类来包装Callable对象
- 3)使用FutrueTask对象作为Thread的target创建并启动新线程
- 4)使用FutrueTask的get方法获取执行结束后的返回值
2、示例代码
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ImplCallable {
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyThread3 m1 = new MyThread3();
FutureTask<Integer> ft = new FutureTask<>(m1);
Thread th = new Thread(ft,"A");
th.start();
System.out.println(ft.get());
}
}
class MyThread3 implements Callable<Integer> {
int x=0;
@Override
public Integer call() throws Exception {
for(;x < 10; x++) {
System.out.println("x=" + x+Thread.currentThread().getName());
}
return x;
}
}
3、运行结果
Connected to the target VM, address: '127.0.0.1:54992', transport: 'socket'
x=0A
x=1A
x=2A
x=3A
x=4A
x=5A
x=6A
x=7A
x=8A
x=9A
10
Disconnected from the target VM, address: '127.0.0.1:54992', transport: 'socket'
Process finished with exit code 0
4、总结
多个线程可以共享一个target对象,因此非常适合多个相同线程处理同一份资源的情况,从而将CPU、代码和数据分开,形参清晰的模型