百度云建站网站建设,dw网页制作教案,wordpress ck-video,好的网站分享文章目录 进程与线程多线程的生命周期多线程的实现方式 进程与线程
进程是指运行中的应用程序#xff0c;每一个进程都有自己独立的内存空间#xff1b;线程是指进程中的一个执行流程#xff0c;有时也称为执行情景#xff1b;一个进程可以由多个线程组成#xff0c;即在… 文章目录 进程与线程多线程的生命周期多线程的实现方式 进程与线程
进程是指运行中的应用程序每一个进程都有自己独立的内存空间线程是指进程中的一个执行流程有时也称为执行情景一个进程可以由多个线程组成即在一个进程中可以同时运行多个不同的线程它们分别执行不同的任务当进程内的多个线程同时运行这种运行方式称为并发运行
多线程的生命周期 就绪状态就绪状态的线程又叫做可运行状态表示当前线程具有抢夺CPU时间片的权力CPU时间片就是执行权。当一个线程抢夺到CPU时间片之后就开始执行run方法run方法的开始执行标志着线程进入运行状态。 运行状态run方法的开始执行标志着这个线程进入运行状态当之前占有的CPU时间片用完之后会重新回到就绪状态继续抢夺CPU时间片当再次抢到CPU时间之后会重新进入run方法接着上一次的代码继续往下执行。 阻塞状态当一个线程遇到阻塞事件例如接收用户键盘输入或者sleep方法等此时线程会进入阻塞状态阻塞状态的线程会放弃之前占有的CPU时间片。之前的时间片没了需要再次回到就绪状态抢夺CPU时间片。 锁池在这里找共享对象的对象锁线程进入锁池找共享对象的对象锁的时候会释放之前占有CPU时间片有可能找到了有可能没找到没找到则在锁池中等待如果找到了会进入就绪状态继续抢夺CPU时间片。这个进入锁池可以理解为一种阻塞状态
多线程的实现方式
继承java.lang.Thread类 extends Thread类代表线程类它的最主要的两个方法是 run()包含线程运行时所执行的代码。 start()用于启动线程。
package extendth;
public class Machine extends Thread{public void run(){for(int a0;a50;a)System.out.println(a);}public static void main(String args[]){Machine machinenew Machine();machine.start(); //启动machine线程}
}实现Runnable接口implements
package runimpl;
public class Machine implements Runnable{private int a0;public void run(){for(a0;a50;a){System.out.println(Thread.currentThread().getName():a);try{Thread.sleep(100);}catch(InterruptedException e){throw new RuntimeException(e);}}}public static void main(String args[]){Machine machinenew Machine();Thread t1new Thread(machine);Thread t2new Thread(machine);t1.start();t2.start();}
}线程池实现 先创建一个线程池工具类再调用工具类方法实现。
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;/** 线程池工具类* 使用的话 : 获取线程池: ThreadUtils.getInstance().getExecutor();* 执行任务: ThreadUtils.getInstance().execute(Runnable runnable);* 杀死线程: ThreadUtils.getInstance().remove(Runnable runnable);** */
public class ThreadPoolUtil {/** volatile 保证变量操作的可见性,也防止指令重排序 应用就是双锁检测的单例模式* */private static volatile ThreadPoolUtil mInstance;/*** 核心线程池的数量同时能够执行的线程数量*/private int corePoolSize;/*** 最大线程池数量表示当缓冲队列满的时候能继续容纳的等待任务的数量*/private int maxPoolSize;/*** 存活时间*/private long keepAliveTime 1;private ThreadPoolExecutor executor;public ThreadPoolExecutor getExecutor() {return executor;}private ThreadPoolUtil() {//给corePoolSize赋值当前设备可用处理器核心数*2 1,能够让cpu的效率得到最大程度执行有研究论证的corePoolSize Runtime.getRuntime().availableProcessors() * 2 1;maxPoolSize corePoolSize;TimeUnit unit TimeUnit.HOURS;executor new ThreadPoolExecutor(//当某个核心任务执行完毕会依次从缓冲队列中取出等待任务corePoolSize,// 然后new LinkedBlockingQueueRunnable(),然后maximumPoolSize,但是它的数量是包含了corePoolSize的maxPoolSize,//表示的是maximumPoolSize当中等待任务的存活时间keepAliveTime,unit,//缓冲队列用于存放等待任务Linked的先进先出new LinkedBlockingQueue(),new DefaultThreadFactory(Thread.NORM_PRIORITY, thread-pool-),new ThreadPoolExecutor.AbortPolicy());}/*** 获取线程池单例*/public static ThreadPoolUtil getInstance() {if (mInstance null) {synchronized (ThreadPoolUtil.class) {if (mInstance null) {mInstance new ThreadPoolUtil();}}}return mInstance;}/*** 执行任务*/public void execute(Runnable runnable) {if (executor null) {executor new ThreadPoolExecutor(corePoolSize,maxPoolSize,keepAliveTime,TimeUnit.SECONDS,new LinkedBlockingQueue(),new DefaultThreadFactory(Thread.NORM_PRIORITY, thread-pool-),new ThreadPoolExecutor.AbortPolicy());}if (runnable ! null) {executor.execute(runnable);}}/*** 移除任务*/public void remove(Runnable runnable) {if (runnable ! null) {executor.remove(runnable);}}private static class DefaultThreadFactory implements ThreadFactory {//线程池的计数private static final AtomicInteger POOL_NUMBER new AtomicInteger(1);//线程的计数private final AtomicInteger threadNumber new AtomicInteger(1);private final ThreadGroup group;private final String namePrefix;private final int threadPriority;DefaultThreadFactory(int threadPriority, String threadNamePrefix) {this.threadPriority threadPriority;this.group Thread.currentThread().getThreadGroup();this.namePrefix threadNamePrefix POOL_NUMBER.getAndIncrement() -thread-;}Overridepublic Thread newThread(Runnable r) {Thread thread new Thread(group, r, namePrefix threadNumber.getAndIncrement(), 0);// 返回True该线程就是守护线程守护线程应该永远不去访问固有资源如数据库、文件等。因为它会在任何时候甚至在一个操作的中间发生中断。if (thread.isDaemon()) {thread.setDaemon(false);}thread.setPriority(threadPriority);return thread;}}//测试public static void main(String[] args) {ThreadPoolExecutor newThreadPoolExecutorgetInstance().getExecutor();for (int i 0; i 100; i) {int n i;newThreadPoolExecutor.execute(new Runnable() {Overridepublic void run() {System.out.println(Thread.currentThread().getName() 正在执行任务);}});}}
}使用方法 获取线程池: ThreadUtils.getInstance().getExecutor(); 执行任务: ThreadUtils.getInstance().execute(Runnable runnable); 杀死线程: ThreadUtils.getInstance().remove(Runnable runnable);