一、在学习多线程这块内容时,我们首先应该知道什么叫进程?什么叫线程?它们之间有何关系、有何区别?
进程: 操作系统中一个程序的执行周期称为一个进程。
线程: 一个程序同时执行多个任务。通常,每一个任务就称为一个线程。
关系: 没有进程就没有线程,进程一旦终止,其内的线程也将不复存在
多进程与多线程区别: 本质区别在于,每个进程拥有自己的一整套变量,而线程则共享数据。共享变量使得线程之
间的通信比进程之间通信更有效、更方便。
二、java实现多线程的方法?
继承Thread类 实现 Runnable接口实现 Callable实现
2.1继承Thread类实现多线程
public class MyThread extends Thread {
private String extra;
public MyThread(String extra){
this.extra=extra;
}
@Override
public void run(){
for(int i=0;i<10;i++){
System.out.println(this.extra+",i="+i);
}
}
}
public class Test {
public static void main(String[] args) {
MyThread mythread1=new MyThread("thread1");
MyThread mythread2=new MyThread("thread2");
MyThread mythread3=new MyThread("thread3");
mythread1.start();
mythread2.start();
mythread3.start();
}
}
执行结果:
thread1,i=0
thread1,i=1
thread1,i=2
thread1,i=3
thread1,i=4
thread1,i=5
thread1,i=6
thread1,i=7
thread1,i=8
thread1,i=9
thread3,i=0
thread3,i=1
thread3,i=2
thread3,i=3
thread3,i=4
thread3,i=5
thread3,i=6
thread3,i=7
thread3,i=8
thread3,i=9
thread2,i=0
thread2,i=1
thread2,i=2
thread2,i=3
thread2,i=4
thread2,i=5
thread2,i=6
thread2,i=7
thread2,i=8
thread2,i=9
Process finished with exit code 0
上面的执行结果并不是不变的
谨记每个线程只能调用一次start()方法,多次调用会出现 “Exception in thread “main” java.lang.IllegalThreadStateException” 异常
2.2Runnable()接口实现多线程
public class MyThread1 implements Runnable{
private String extra;
public MyThread1(String extra){
this.extra=extra;
}
@Override
public void run() {
for(int i=0;i<10;i++){
System.out.println(this.extra+",i="+i);
}
}
}
public class Test {
public static void main(String[] args) {
MyThread1 mythread1=new MyThread1("thread1");
MyThread1 mythread2=new MyThread1("thread2");
MyThread1 mythread3=new MyThread1("thread3");
new Thread(mythread1).start();
new Thread(mythread2).start();
new Thread(mythread3).start();
}
}
注意!!!
这里就不能 mythread1.start() 这样写了,因为此时MyThread类继承的不再是Thread类而实现了Runnable接口,虽然解决了单继承局限问题,但是start()方法没有被继承了。那么此时就需要关注 Thread 类提供的构造方法public Thread(Runnable target) 来接收Runnable的接口对象
结果如下:
thread1,i=0
thread1,i=1
thread3,i=0
thread3,i=1
thread3,i=2
thread3,i=3
thread3,i=4
thread3,i=5
thread3,i=6
thread3,i=7
thread3,i=8
thread3,i=9
thread2,i=0
thread2,i=1
thread1,i=2
thread1,i=3
thread1,i=4
thread1,i=5
thread1,i=6
thread1,i=7
thread1,i=8
thread1,i=9
thread2,i=2
thread2,i=3
thread2,i=4
thread2,i=5
thread2,i=6
thread2,i=7
thread2,i=8
thread2,i=9
Process finished with exit code 0
2.2.1Thread和Runnable的比较
- 从使用形式来讲,使用Runnable实现多线程要比继承Thread类要好,因为可以避免但继承局限。
- 在生活中应用中,使用Runnable实现的多线程的程序类可以更好的描述出程序共享的概念在生活中应用中,使用Runnable实现的多线程的程序类可以更好的描述出程序共享的概念
比如在12306在购买票:
public class MyThread3 extends Thread {
private int ticket = 10; // 一共10张票
@Override
public void run() {
while (this.ticket > 0) {
System.out.println("剩余票数:" + this.ticket--);
}
}
public class Test {
public static void main(String[] args) {
new MyThread3().start();
new MyThread3().start();
new MyThread3().start();
}
}
结果如下:
剩余票数:10
剩余票数:9
剩余票数:8
剩余票数:7
剩余票数:6
剩余票数:5
剩余票数:10
剩余票数:4
剩余票数:3
剩余票数:2
剩余票数:1
剩余票数:9
剩余票数:8
剩余票数:7
剩余票数:6
剩余票数:5
剩余票数:4
剩余票数:3
剩余票数:2
剩余票数:1
剩余票数:10
剩余票数:9
剩余票数:8
剩余票数:7
剩余票数:6
剩余票数:5
剩余票数:4
剩余票数:3
剩余票数:2
剩余票数:1
Process finished with exit code 0
从上面的执行结果看,三个线程各自卖了各自的10个票,而不是我们要求看到卖一个总体10个的票。因此我们希望在12306看到的结果肯定不能是这样的。因此我们就需要Runnable来实现数据共享。
public class MyThread4 implements Runnable {
private int ticket = 10 ; // 一共10张票
@Override
public void run() {
while(this.ticket>0){
System.out.println("剩余票数:"+this.ticket -- );
}
}
}
public class MyThread4 implements Runnable {
private int ticket = 10 ; // 一共10张票
@Override
public void run() {
while(this.ticket>0){
System.out.println("剩余票数:"+this.ticket -- );
}
}
}
结果如下:
剩余票数:10
剩余票数:9
剩余票数:8
剩余票数:7
剩余票数:6
剩余票数:5
剩余票数:4
剩余票数:3
剩余票数:2
剩余票数:1
Process finished with exit code 0
2.3Callable实现多线程
Runnable中的run()方法没有返回值,它的设计也遵循了主方法的设计原则:线程开始了就别回头。但是往往在实际应用中我们需要一些返回值,例如某些线程执行完成后可能带来一些返回结果,这种情况下就只能利用Callable来实现多线程。
import java.util.concurrent.Callable;
public class MyThread5 implements Callable<String> {
private int ticket = 10 ; // 一共10张票
@Override
public String call() throws Exception {
while(this.ticket>0){
System.out.println("剩余票数:"+this.ticket -- );
}
return "票卖完了,下次吧。。。" ;
}
}
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class test {
public static void main(String[] args) throws Exception {
FutureTask<String> task = new FutureTask<>(new MyThread5()) ;
new Thread(task).start();
new Thread(task).start();
System.out.println(task.get());
}
}
结果如下:
剩余票数:10
剩余票数:9
剩余票数:8
剩余票数:7
剩余票数:6
剩余票数:5
剩余票数:4
剩余票数:3
剩余票数:2
剩余票数:1
票卖完了,下次吧。。。
Process finished with exit code 0