进程与线程
Java支持多线程
传统DOS采用的是单进程处理,导致病毒入侵无法处理,同一时间只能有一个进程处理。windows开启多进程设计后,可以实现并发。
线程是更小的程序执行单元,是在进程的基础上创建和使用的。
Java是多线程的处理语言。
Thread类实现多线程
Java中提供了java.lang.Thread,继承这个类就表示线程的主体类,Thread类中提供的run()方法为线程的主方法。
正常情况下,如果想要使用类中的方法,需要先产生一个实例化对象,然后调用。run()方法却不能直接调用。所以要想启动多线程,必须使用start()完成。
package wzr.study01.Thread;
public class ThreadTest {
public static void main(String[] args) {
// new MyThread("进程A").run();
// new MyThread("进程B").run();
// new MyThread("进程C").run();
//此时程序中的线程只是顺序执行,没有产生交替执行
new MyThread("进程A").start();
new MyThread("进程B").start();
new MyThread("进程C").start();
}
}
class MyThread extends Thread{
private String title;
private int starttime;
MyThread(String title){
starttime=0;
this.title=title;
}
public void run() {
for(int x=0;x<10;x++) {
System.out.println("MyThread" + this.title + " start time:" + starttime++);
}
}
}
虽然调用的start()方法,但是执行的是run()方法。而且是交替执行。
start()方法的实现:
public synchronized void start() {
if (threadStatus != 0)
throw new IllegalThreadStateException();
group.add(this);
boolean started = false;
try {
start0();//在start方法里调用start0方法,start0只定义了方法名称,没有实现
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
其抛出了一个异常:IllegalThreadStateException,但是没有throws或者try…catch,它是RuntimeException的子类。
每一个线程类只可以启动一次,如果重复启动,则抛出IllegalThreadStateException异常。
考虑到不同层次的开发者需求,其支持本地操作系统函数调用,这项技术被称为JNI(java本地接口)技术,java开发中不推荐使用,利用这项技术,可以提供操作系统底层函数,进行一些特殊的处理,Thread类里面就需要start0方法依赖与不同的操作系统实现,但是不兼容。所以只能用start方法
Runnable接口实现多线程
java中对于继承有单继承局限,第二种主体定义实现结构形式:java.lang.Runnable接口
接口定义如下:
@FunctionalInterface
public interface Runnable{
public void run();
}
它没有start方法,所以无法直接启动,而Thread类的构造方法有:public Thread(Runnable target);。所以可以借用Thread的start方法:
package wzr.study01.Thread;
public class ThreadTest {
public static void main(String[] args) {
Thread threadA=new Thread(new MyThreadRun("线程A"));
Thread threadB=new Thread(new MyThreadRun("线程B"));
Thread threadC=new Thread(new MyThreadRun("线程C"));
threadA.start();
threadB.start();
threadC.start();
}
}
class MyThreadRun implements Runnable{
private String title;
private int starttime;
public MyThreadRun(String title){
starttime=0;
this.title=title;
}
public void run() {
for(int x=0;x<10;x++) {
System.out.println("MyThread" + this.title + " start time:" + starttime++);
}
}
}
此时就是不具有单继承的局限,标准设计
jdk1.8开始Runnable使用了函数式接口定义,所以可以直接利用lamda表达式进行线程类的实现:
public static void main(String[] args) {
for(int x=0;x<3;x++) {
String title="线程对象:"+x;
Runnable run=()->{
for(int y=0;y<10;y++) {
System.out.println(title + " y:" + y);
}
};
new Thread(run).start();
}
}
常规:
public static void main(String[] args) {
for(int x=0;x<3;x++) {
String title="线程对象:"+x;
new Thread(()->{
for(int y=0;y<10;y++) {
System.out.println(title + " y:" + y);
}
}).start();
}
}
所以,开发中应该优先考虑Runnable接口实现,永远通过Thread类对象启动多线程。
Thread与Runnable的区别
从结构本身来讲,Runnable可以更好的避免功能的扩充,并且避免单继承的局限。
从结构上看:
- Thread的定义:public class Thread extends Object implements Runnable{}
- Thread接收的对象target,run方法定义是直接调用Runnable接口的子类。
多线程是多个对象对同一资源的抢占。Thread类主要描述线程,而Runnable子类主要描述资源,下面举一个售票系统的例子:
package wzr.study01.Thread;
public class Cashier {
public static void main(String[] args) {
MyTicket mt=new MyTicket("sell");
new Thread(mt).start();//线程启动
new Thread(mt).start();//线程启动
new Thread(mt).start();//线程启动
}
}
class MyTicket implements Runnable{//线程主体类
private int ticket=5;
private String title;
public MyTicket(String title){
this.title=title;
}
public void run() {
for(int i=0;i<10;i++) {
if(ticket>0) {
System.out.println(title+":"+ticket--);
}
}
}
}
Callable接口实现多线程
Runnable接口缺陷:线程执行结束后无法获取一个返回值。
jdk1.5之后,线程实现接口:java.util.concurrent.Callable接口。
Callable接口定义:
@FunctionalInterface
public interface Callable<V>{
public V call() throws Exception;
}
Callable设置的泛型为返回的数据类型,避免向下转型带来的安全隐患。
Callable与Thread的联系:
代码案例:
package wzr.study01.Thread;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class Realize {
public static void main(String[] args) throws Exception{
FutureTask<String> task=new FutureTask<String>(new MyThreadRealize());
new Thread(task).start();
System.out.println("【future返回值】:"+task.get());
}
}
class MyThreadRealize implements Callable<String>{
@Override
public String call() throws Exception {
for(int i=0;i<10;i++) {
System.out.println("call实现类开始执行:"+i);
}
return "【执行完毕】";
}
}
Runnable与Callable的区别:
Runnable是jdk1.0后提出的,Callable是jdk1.5以后提出的。
Runnable接口唯一的方法run(),没有返回值;
Callable接口唯一的方法call(),有返回值。
多线程的运行状态
- 当执行完Thread.start()方法后,线程并没有开始执行,而是都进入就绪状态。
- 线程处理有自己的运行状态(就绪,阻塞,运行),创建使用start()会进入就绪状态。
- 进入就绪队列就会开始等待资源调度。CPU调度。
- run()方法执行完毕后,线程任务结束。进程终止。