网站建设及服务招标公告,手机排行榜第一名,茂名企业建站程序,旅游景点网站设计方案目录
1.Tread类介绍
2线程的构造方法——创建线程
1.继承Thread类#xff0c;重写run()方法
2.使用Runnbable接口创建线程
3.继承 Thread, 重写 run, 使用匿名内部类
4.实现 Runnable, 重写 run, 使用匿名内部类
5.使用 lambda 表达式#xff08;重点掌握#xff09;…目录
1.Tread类介绍
2线程的构造方法——创建线程
1.继承Thread类重写run()方法
2.使用Runnbable接口创建线程
3.继承 Thread, 重写 run, 使用匿名内部类
4.实现 Runnable, 重写 run, 使用匿名内部类
5.使用 lambda 表达式重点掌握
3.Tread类常见方法解读
3.1Tread类常见构造方法
3.2 Tread类的几个常见属性
3.3启动一个线程-start()方法
3.4中断一个线程
3.5等待一个线程-join()
3.6休眠线程
3.7 实现一个简单的多线程
4.线程的状态
4.1线程的六种状态
4.2线程状态和状态转移 1.Tread类介绍
Thread 类是 JVM 用来管理线程的一个类换句话说每个线程都有一个唯一的 Thread 对象与之关联每个执行流线程也需要有一个对象来描述 Thread 类的对象就是用来描述一个线程执行流的JVM 会将这些 Thread 对象组织起来用于线程调度、线程管理。
2线程的构造方法——创建线程 无论使用哪一个方法创建线程我们都需要将其中的run方法重写run方法中写入的是线程需要执行的任务它的作用就相当于主线程的main方法。 1.继承Thread类重写run()方法
这个构造方法没有参数通过创造一个类继承Thread类来实现线程 线程必须重写run()方法才算完成。当我们new一个线程时此时的线程还没开始执行再用创建的类调用start()方法才算运行了这个线程。
class TestTread extends Thread{Overridepublic void run() {System.out.println(this tread1);}
}
public class testDemo2 {public static void main(String[] args) {Thread threadnew TestTread();thread.start();}}2.使用Runnbable接口创建线程
我们可以看出该构造方法的参数类型是一个接口类因此我们需要创建一个类来实现这个接口再new一个实现Runnable的类对象再new一个线程将之前创建的对象放入到参数这里。
class TestTread2 implements Runnable {Overridepublic void run() {System.out.println(this t2);}
}
public class testDemo2 {public static void main(String[] args) {//new一个实现Runnable接口的对象TestTread2 testTread2new TestTread2();//传入参数完成线程的创建Thread t2new Thread(testTread2);t2.start();}}3.继承 Thread, 重写 run, 使用匿名内部类 使用匿名内部类可以省略创建对象的过程可以减少资源消耗。
public class testDemo3 {public static void main(String[] args) {//通过Thread匿名内部类的方法创建一个线程Thread t1new Thread() {Overridepublic void run() {System.out.println(this t1);}};t1.start();}}4.实现 Runnable, 重写 run, 使用匿名内部类
这个和3一样可以少创建一个对象减少资源的消耗。
public class testDemo4 {public static void main(String[] args) {Thread t1new Thread(new Runnable() {Overridepublic void run() {System.out.println(this t1);}});t1.start();}
}5.使用 lambda 表达式重点掌握
它的实现原理和匿名内部类相似这点不了解lambda表达式的可以去看一下如何使用。
public class testDemo4 {public static void main(String[] args) Thread t2new Thread(()-{System.out.println(this t2);});t2.start();}
}3.Tread类常见方法解读
3.1Tread类常见构造方法
方法说明Thread()创建线程对象Thread(Runnable target)使用 Runnable 对象创建线程对象Thread(String name)创建线程对象并命名Thread(Runnable target, String name)使用 Runnable 对象创建线程对象并命名【了解】Thread(ThreadGroup group, Runnable target)线程可以被用来分组管理分好的组即为线程组这 个目前我们了解即可
3.2 Tread类的几个常见属性
属性获取方法IDgetId()名称getName()状态getState()优先级getPriority()是否后台线程isDaemon()是否存活isAlive()是否被中断isInterrupted(
ID 是线程的唯一标识不同线程不会重复。名称是各种调试工具用到。状态表示线程当前所处的一个情况下面我们会进一步说明。优先级高的线程理论上来说更容易被调度到。关于后台线程需要记住一点JVM会在一个进程的所有非后台线程结束后才会结束运行。是否存活即简单的理解为 run 方法是否运行结束了。线程的中断问题下方会有解析
3.3启动一个线程-start()方法
调用 start 方法, 才算是在操作系统的底层创建出一个线程.
public class testDemo4 {public static void main(String[] args) {Thread t1new Thread(new Runnable() {Overridepublic void run() {System.out.println(this t1);}});t1.start();}
}3.4中断一个线程
方法说明public void interrupt()中断对象关联的线程如果线程正在阻塞则以异常方式通知 否则设置标志位public static boolean interrupted()判断当前线程的中断标志位是否设置调用后清除标志位public boolean isInterrupted()判断对象关联的线程的标志位是否设置调用后不清除标志位
使用 thread 对象的 interrupted() 方法通知线程结束
public class testDemo6 {public static void main(String[] args) throws InterruptedException {Thread t1new Thread(() -{while (!Thread.interrupted()) {System.out.println(Thread.currentThread().getName());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();break;}}});t1.start();Thread.sleep(1000*2);t1.interrupt();}
}t1收到通知的方式有两种: 1.如果线程因为调用 wait/join/sleep 等方法而阻塞挂起则以 InterruptedException 异常的形式通知清除中断标志. 当出现 InterruptedException 的时候, 要不要结束线程取决于 catch 中代码的写法. 可以选择忽略这个异常, 也可以跳出循环结束线程(加break). 2.否则只是内部的一个中断标志被设置thread 可以通过 Thread.interrupted() 判断当前线程的中断标志被设置清除中断标志Thread.currentThread().isInterrupted() 判断指定线程的中断标志被设置不清除中断标志使用 Thread.isInterrupted() , 线程中断会清除标志位标志位是否清除, 就类似于一个开关. Thread.Interrupted() 相当于按下开关, 开关自动弹起来了. 这个称为 清除标志位. public class ThreadDemo {public static void main(String[] args) throws InterruptedException {Thread thread new Thread(()-{for (int i 0; i 10; i) {// //打印标准位System.out.println(Thread.interrupted());}});thread.start();thread.interrupt();}
}
结果
观察结果可以看出已知interrupted()初始是true之后就打印的值就是false因为标志位已经被删除了。
使用 Thread.currentThread().isInterrupted() , 线程中断标记位不会清除标志位是否清除, 就类似于一个开关. Thread.currentThread().isInterrupted() 相当于按下开关之后, 开关弹不起来, 这个称为不清除标志位. public class ThreadDemo {public static void main(String[] args) throws InterruptedException {Thread thread new Thread(()-{for (int i 0; i 10; i) {// //打印标准位System.out.println(Thread.currentThread().isInterrupted());}});thread.start();thread.interrupt();}
}
结果
观察结果可以清晰的看出打印的全是true这是因为标志位没有被删除它的值还是true。
3.5等待一个线程-join()
方法说明public void join()等待线程结束public void join(long millis)等待线程结束最多等 millis 毫秒public void join(long millis, int nanos)同理但可以更高精度
public class testDemo7 {public static void main(String[] args) throws InterruptedException {Thread t1new Thread(()-{for (int i 0; i 10; i) {System.out.println(第i次打印Thread.currentThread().getName());}System.out.println(----------);});Thread t2new Thread(()-{for (int i 0; i 10; i) {System.out.println(第i次打印Thread.currentThread().getName());}});t1.start();//t2等待t1执行完毕t2才可执行t1.join();t2.start();}
}结果 可以看出使用了join()两个线程不再是杂乱运行了而是先运行完t1线程再运行的t2线程。这就是join 的作用。
3.6休眠线程
这个是比较熟悉一组方法有一点要记得因为线程的调度是不可控的所以这个方法只能保证实际休眠时间是大于等于参数设置的休眠时间的。
方法说明public static void sleep(long millis) throws InterruptedException休眠当前线程 millis 毫秒public static void sleep(long millis, int nanos) throws InterruptedException可以更高精度的休眠
3.7 实现一个简单的多线程 这两个线程任务都是打印自己的名字其中使用的currentThread()方法是或者当前线程的引用getName()方法就是或者线程的名字(就算我们没有给线程起名字系统也会给它自定义一个名字)。
public class testDemo5 {public static void main(String[] args) {Thread t1new Thread(()-{while (true) {//打印线程名称System.out.println(Thread.currentThread().getName());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});Thread t2new Thread(()-{while (true){//打印线程名称System.out.println(Thread.currentThread().getName());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});//执行线程任务t1.start();t2.start();}
}代码实现结果
观察可以看出两个线程是交叉运行并且是杂乱运行好不规律可言。没错线程的并发就是没有规律的谁先运行取决于操作系统如何调度线程因此线程是抢占式执行。
4.线程的状态
4.1线程的六种状态
NEW: 安排了工作, 还未开始行动刚刚创建一个Tread对象还没开始工作。RUNNABLE: 可工作的. 又可以分成正在工作中和即将开始工作正在CPU上执行任务或者在就绪队列中随时可以在CPU上执行的。BLOCKED: 这几个都表示排队等着其他事情synchronized加锁。WAITING: 这几个都表示排队等着其他事情。TIMED_WAITING: 这几个都表示排队等着其他事情使用sleep或者join方法引起的。TERMINATED: 工作完成了。
4.2线程状态和状态转移 举个例子看下图
文章转载自: http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.hcwlq.cn.gov.cn.hcwlq.cn http://www.morning.nxstj.cn.gov.cn.nxstj.cn http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn http://www.morning.xrpwk.cn.gov.cn.xrpwk.cn http://www.morning.gllhx.cn.gov.cn.gllhx.cn http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.msbct.cn.gov.cn.msbct.cn http://www.morning.chgmm.cn.gov.cn.chgmm.cn http://www.morning.skrh.cn.gov.cn.skrh.cn http://www.morning.ykklw.cn.gov.cn.ykklw.cn http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn http://www.morning.phjny.cn.gov.cn.phjny.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.djwpd.cn.gov.cn.djwpd.cn http://www.morning.ymfzd.cn.gov.cn.ymfzd.cn http://www.morning.sbrrf.cn.gov.cn.sbrrf.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.mdwlg.cn.gov.cn.mdwlg.cn http://www.morning.ysbrz.cn.gov.cn.ysbrz.cn http://www.morning.gydsg.cn.gov.cn.gydsg.cn http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.bsbcp.cn.gov.cn.bsbcp.cn http://www.morning.nlnmy.cn.gov.cn.nlnmy.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.mrpqg.cn.gov.cn.mrpqg.cn http://www.morning.kybyf.cn.gov.cn.kybyf.cn http://www.morning.sfnr.cn.gov.cn.sfnr.cn http://www.morning.nkqrq.cn.gov.cn.nkqrq.cn http://www.morning.mpflb.cn.gov.cn.mpflb.cn http://www.morning.skdrp.cn.gov.cn.skdrp.cn http://www.morning.nlrp.cn.gov.cn.nlrp.cn http://www.morning.fsnhz.cn.gov.cn.fsnhz.cn http://www.morning.fjlsfs.com.gov.cn.fjlsfs.com http://www.morning.hxxzp.cn.gov.cn.hxxzp.cn http://www.morning.xhrws.cn.gov.cn.xhrws.cn http://www.morning.prysb.cn.gov.cn.prysb.cn http://www.morning.hhnhb.cn.gov.cn.hhnhb.cn http://www.morning.dbylp.cn.gov.cn.dbylp.cn http://www.morning.gfmpk.cn.gov.cn.gfmpk.cn http://www.morning.tdqhs.cn.gov.cn.tdqhs.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn http://www.morning.mnrqq.cn.gov.cn.mnrqq.cn http://www.morning.jjnql.cn.gov.cn.jjnql.cn http://www.morning.bsplf.cn.gov.cn.bsplf.cn http://www.morning.jzkqg.cn.gov.cn.jzkqg.cn http://www.morning.fnfhs.cn.gov.cn.fnfhs.cn http://www.morning.pnjsl.cn.gov.cn.pnjsl.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.qmxsx.cn.gov.cn.qmxsx.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.rnzjc.cn.gov.cn.rnzjc.cn http://www.morning.qnbsx.cn.gov.cn.qnbsx.cn http://www.morning.rtspr.cn.gov.cn.rtspr.cn http://www.morning.tkcz.cn.gov.cn.tkcz.cn http://www.morning.mkzdp.cn.gov.cn.mkzdp.cn http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.qkrgk.cn.gov.cn.qkrgk.cn http://www.morning.qlck.cn.gov.cn.qlck.cn http://www.morning.pcqdf.cn.gov.cn.pcqdf.cn http://www.morning.lwzpp.cn.gov.cn.lwzpp.cn http://www.morning.zjcmr.cn.gov.cn.zjcmr.cn http://www.morning.wmmtl.cn.gov.cn.wmmtl.cn http://www.morning.xykst.cn.gov.cn.xykst.cn http://www.morning.dwztj.cn.gov.cn.dwztj.cn http://www.morning.khfk.cn.gov.cn.khfk.cn http://www.morning.mhfbp.cn.gov.cn.mhfbp.cn http://www.morning.hympq.cn.gov.cn.hympq.cn http://www.morning.dmthy.cn.gov.cn.dmthy.cn http://www.morning.rkqqf.cn.gov.cn.rkqqf.cn http://www.morning.nsmyj.cn.gov.cn.nsmyj.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.fzqfb.cn.gov.cn.fzqfb.cn http://www.morning.rzbgn.cn.gov.cn.rzbgn.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.fnhxp.cn.gov.cn.fnhxp.cn http://www.morning.c7498.cn.gov.cn.c7498.cn