-
进程,线程概念
进程:os中的一个程序的执行周期,例如打开浏览器到关闭浏览器
线程:进程中的一个任务,一个进程中包含N个线程,例如不同的标签页 -
进程,线程的区别
1.每一个进程拥有自己的一整套变量,是操作系统中资源分配的最小单位,线程依托于进程存在,并且多个线程共享进程的资源,线程是os中任务调度的基本变量
2.启动撤销一个进程的开销要比启动撤销一个线程大的多(线程轻量级)
3.没有进程就没有线程,进程一旦终止,其内的线程全部撤销 -
高并发
同一时刻线程的访问量非常高
DDos:僵尸设备,例如摄像头 -
线程的状态
不管何种情况。如果要想启动多线程只有Thread类中的start()方法。 -
java多线程实现
1.继承Thread类实现多线程
java.lang.Thread是线程操作的核心类,JDK1.0提供
新建一个线程最简单的方法就是直接继承Thread类而后覆写类中的run()方法(相当于主方法)无论哪种方法实现多线程,线程启动一律调用Thread类提供的start方法直接调用run方法就是直接调用了普通方法而已
class MyThread extends Thread {
private String title;
public MyThread(String title){
this.title = title;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(this.title+","+i);
}
}
}
public class Test1 {
public static void main(String[] args) {
Thread thread1 = new MyThread("A");
Thread thread2 = new MyThread("B");
thread1.start();
thread2.start();
}
}
start方法解析
public synchronized void start() {
if (threadStatus != 0)
throw new IllegalThreadStateException();
group.add(this);
boolean started = false;
try {
start0();//真正将线程启动核心线程启动方法
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
}
}
}
a)首先检查线程状态是否为0(线程默认状态为0表示未启动)如果线程已经启动再次调用start方法会抛出IllegalThreadStateException(非受查异常)。一个线程的start方法只能调用一次
b)private native void start0(); 通过start0真正将线程启动核心线程启动方法,为本地方法
c)JVM调用start0方法进行系统分配资源和资源调度,准备好资源启动线程后回调run方法来执行线程的具体任务
2.Runnable接口实现多线程
@FunctionalInterface //检查当前接口中是否只有一个抽象方法(覆写)
public interface Runnable {
public abstract void run();
}
Thread类的核心功能是进行线程的启动。如果一个类为了实现多线程直接去继承Thread类就会有但继承局限。在java中又提供有另外一种实现模式:Runnable接口。
MyThread是一个实现Runnable接口的子类,Thread类才能将线程启动,因此通过构造注入的方法将MyThread类的对象传给Thread类,Thread类有直接的构造方法可以接收Runnable对象
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
class MyThread implements Runnable {
private String title;
public MyThread(String title){
this.title = title;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(this.title+","+i);
}
}
}
public class Test1 {
public static void main(String[] args) {
MyThread thread1 = new MyThread("A");
MyThread thread2 = new MyThread("B");
Thread threadA = new Thread(thread1);
Thread threadB = new Thread(thread2);
threadA.start();
threadB.start();
}
}
Runnable与Thread的区别:
a)Runnable避免了单继承局限
b)Java中多线程的处理就是一个典型的代理模式,其中Thread类完成资源调度,系统分配辅助线程业务类;自定义的线程类负责真实业务实现
class MyThread implements Runnable//真实业务
class Thread implements Runnable {
代理设计模式
Thread是辅助完成资源调度,线程启动,真正做的事情在自己实现的里面
(可以看出Thread是Runnable接口的子类,一定覆写了Runnable接口的run方法)
c)使用Runnable接口实现的多线程程序类可以更好的描述资源共享
class MyThread extends Thread {
private String title;
private int ticket = 10;
public MyThread(String title){
this.title = title;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(this.title+"还剩下"+ticket--+"票");
}
}
}
public class Test1 {
public static void main(String[] args) {
Thread thread1 = new MyThread("黄牛A");
Thread thread2 = new MyThread("黄牛B");
thread1.start();
thread2.start();
}
}
(A,B各卖各的10张票,因为ticket是成员变量,与对象有关)
因此用Runnab接口,可以实现一个MyTread类的对象可以共享给多个线程:
class MyThread implements Runnable {
private String title;
private int ticket = 10;
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(this.title+"还剩下"+ticket--+"票");
}
}
}
public class Test1 {
public static void main(String[] args) {
MyThread thread = new MyThread();
Thread thread1 = new Thread(thread);
Thread thread2 = new Thread(thread);
thread1.start();
thread2.start();
}
}
通过Thread实现多线程一个任务只能启动一个线程,Runnable实现时一个任务可以启动多个线程
3.Callable接口实现多线程
@FunctionalInterface
public interface Callable {
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
V call() throws Exception;
}
线程执行后有返回值V:
V call() throwsException:
java.util.Future接口中的get方法取得Callable接口call方法的返回值
V get() throws InterruptedException, ExecutionException;
RunnableFuture 接口实现了Runnable和Future两个父接口:
RunnableFuture extends Runnable, Future
FutureTask类实现了RunnableFuture 接口,而它里面的构造方法可以接收Callable对象
public FutureTask(Callable callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
class MyThread implements Callable {
private String title;
private int ticket = 10;
@Override
public String call() {
for (int i = 0; i < 10; i++) {
System.out.println("还剩下"+ticket--+"票");
}
return "票卖完了,客官明天见";
}
}
public class Test1 {
public static void main(String[] args) {
Callable callable = new MyThread();
FutureTask futureTask = new FutureTask<>(callable);
Thread thread = new Thread(futureTask);
thread.start();
try {
System.out.println(futureTask.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
当线程需要返回值时只能采用Callable接口实现多线程