#多线程
#1.多线程的创建方式
##1. 方式1:继承于Thread类:
1.创建一个Thread的子类继承Thread;
2.重写其中的run()方法;
3.实例化继承Thread的实例对象;
4.通过此实例对象调用start()方法
public class myThread extends Thread{
private String title;
public myThread(String title) {
this.title=title;
}
@Override
public void run() {
for(int x=0;x<10;x++) {
System.out .println(this.title+"第"+x+"次运行");
}
}
}
public class test{
public static void main(String[] args) {
new myThread("线程A").start();
new myThread("线程B").start();
new myThread("线程C").start();
}
}
start与run方法的区别:
start方法的作用:1.启动当前线程 2.调用当前线程的重写的run方法(在主线程中生成子线程,有两条线程)
调用start方法以后,一条路径代表一个线程,同时执行两线程时,因为时间片的轮换,所以执行过程随机分配,且一个线程对象只能调用一次start方法。
run方法的作用:在主线程中调用以后,直接在主线程一条线程中执行了该线程中run的方法。(调用线程中的run方法,只调用run方法,并不新开线程)
#2. 方式2:实现Runable接口方式
1.创建一个实现了Runable接口的类
2.实现类去实现Runnable中的抽象方法:run()
3.创建实现类的对象
4.将此对象作为参数传递到Thread类中的构造器中,创建Thread类的对象
5.通过Thread类的对象调用start()
public class myThread implements Runnable{
private String title;
public myThread(String title) {
this.title=title;
}
@Override
public void run() {
for(int x=0;x<10;x++) {
System.out .println(this.title+"第"+x+"次运行");
}
}
}
public class test{
public static void main(String[] args) {
Thread mt1=new Thread(new myThread("线程A"));
Thread mt2=new Thread(new myThread("线程B"));
Thread mt3=new Thread(new myThread("线程C"));
mt1.start();
mt2.start();
mt3.start();
}
}
比较创建线程的两种方式:
开发中,优先选择实现Runable接口的方式
原因1:实现的方式没有类的单继承性的局限性
2:实现的方式更适合用来处理多个线程有共享数据的情况
联系:Thread也是实现自Runable,两种方式都需要重写run()方法,将线程要执行的逻辑声明在run中
#3 Callable接口实现多线程
1.创建一个实现callable的实现类
- 2.实现call方法,将此线程需要执行的操作声明在call()中
- 3.创建callable实现类的对象
- 4.将callable接口实现类的对象作为传递到FutureTask的构造器中,创建FutureTask的对象
- 5.将FutureTask的对象作为参数传递到Thread类的构造器中,创建Thread对象,并调用start方法启动(通过FutureTask的对象调用方法get获取线程中的call的返回值)
import java.util.concurrent.Callable;
public class myThread implements Callable<String> {
public String call() throws Exception {
for(int x=0;x<10;x++) {
System.out.println("线程执行"+x);
}
return "线程执行完毕";
}
}
import java.util.concurrent.FutureTask;
public class test{
public static void main(String[] args) throws Exception {
FutureTask<String> task=new FutureTask<>(new myThread());
new Thread(task).start();
System.out.println("线程返回数据"+task.get());
}
}
#强制执行
join主要用于阻塞其他线程,待此线程执行完后,再执行其他线程。
public class myThread implements Runnable {
@Override
public void run() {
for(int x=0;x<10;x++) {
System.out.println("执行程序"+x);
}
}
}
public class test{
public static void main(String[] args) throws InterruptedException {
myThread th=new myThread();
Thread tt=new Thread(th);
tt.start();
for(int i=0;i<10;i++) {
if(i==5) {
tt.join();
}
System.out.println("强制执行"+i);
}
}
}
#线程礼让
yield 用于暂停当前正在执行的线程,让其他线程执行。
但是不一定有效果
因为让出后,让步的线程可能再次被CPU调度分给这个线程。
先来看没有使用yield,都是按顺序执行。
public class ThreadDemo implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"===正在执行");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"===停止");
}
public static void main(String[] args) throws InterruptedException {
ThreadDemo thread = new ThreadDemo();
new Thread(thread,"小蓝").start();
new Thread(thread,"一颗剽悍的种子").start();
}
}