示例代码@slf4j(topic = "c.test4")public class test4 { public static void main(string[] args) { thread t1 = new thread("t1"){ @override public void run() { log.debug("running"); } }; t1.run(); }}
上述代码是直接调用的$run()$方法。可以看到打印信息里,是$main$线程执行了这个方法。
@slf4j(topic = "c.test4")public class test4 { public static void main(string[] args) { thread t1 = new thread("t1"){ @override public void run() { log.debug("running"); } }; t1.start(); }}
而如果使用$start()$方法启动,才是真正的由$t1$线程执行的$run$方法。
注意需要注意的是,当$thread$对象调用了$start()$方法后,就会进入就绪状态,处于就绪状态时无法再调用$start()$方法,否则就会抛出$illegalthreadstateexception$异常,如下代码所示
@slf4j(topic = "c.test4")public class test4 { public static void main(string[] args) { thread t1 = new thread("t1"){ @override public void run() { log.debug("running"); } }; t1.start(); t1.start(); }}
异常信息:
sleep方法与yield方法sleep调用$sleep()$方法会让当前线程从$running$状态变成$time waiting$状态(阻塞)
其它线程可以使用$interrupt$方法打断正在睡眠的线程,此时$sleep$方法会抛出interruptedexception
睡眠结束后的线程未必会立刻得到执行
建议用$timeunit$的$sleep$代替$thread$的$sleep$来获得更好的可读性示例代码
@slf4j(topic = "c.test5")public class test5 { public static void main(string[] args) { thread t1 = new thread("t1"){ @override public void run() { try { thread.sleep(2000); } catch (interruptedexception e) { e.printstacktrace(); } } }; t1.start(); log.debug("t1 state {}", t1.getstate()); //让主线程休眠500ms try { thread.sleep(500); } catch (interruptedexception e) { e.printstacktrace(); } log.debug("t1 state {}", t1.getstate()); }}//17:13:21.729 [main] debug c.test5 - t1 state runnable//17:13:22.245 [main] debug c.test5 - t1 state timed_waiting
上述代码中,首先启动$t1$线程,此时打印线程的状态应该是处于$runnable$状态,而让主线程休眠是防止主线程先执行打印,但是还未进入到$sleep()$状态。当执行到$run()$里边的$sleep$方法时,线程进入$timed waiting$状态
@slf4j(topic = "c.test6")public class thread6 { public static void main(string[] args) throws interruptedexception { thread t1 = new thread("t1") { @override public void run() { try { log.debug("enter sleep"); thread.sleep(2000); } catch (interruptedexception e) { log.debug("wake up"); e.printstacktrace(); } } }; t1.start(); thread.sleep(1000); log.debug("interrupt t1"); //被唤醒 t1.interrupt(); }}
执行结果
上述代码中,当$start$方法启动后,$t1$线程进入睡眠状态,打印提示信息,睡眠时间为$2s$,在$main$线程中睡眠$1s$后打断$t1$线程的睡眠,提示打断信息,并且调用$interrupt()$方法,此时线程被打断,抛出异常。
$timeunit$类中新增了以什么单位去睡眠,可读性更好,但是本质上没区别,只是进行了单位换算
timeunit.seconds.sleep(1);//该语句作用是睡眠一秒
yield调用$yield$会让当前进程从$running$进入到$runnable$就绪状态,然后调度执行其他线程具体的实现依赖于操作系统的任务调度器,(即当任务调度器中没有其他任务时,即使让出$cpu$,也会继续执行该线程)$sleep$执行后是进入阻塞状态,此时睡眠时间不结束,就不会分配$cpu$给该线程,但是$yield$是进入就绪状态,即如果没有其他线程需要执行,那么还会给该线程分配时间片,这是$sleep$和$yield$的最大区别线程优先级
线程优先级会提示调度器优先调度该线程,但它仅仅是一个提示,调度器可以忽略他
如果$cpu$比较忙,那么优先级高的会获得更多的时间片,可$cpu$空闲时,优先级几乎没有
sleep的应用-防止cpu占用100%在没有利用$cpu$来计算时,不要让$while(true)$空转浪费$cpu$,这时可以可以使用$yield$或者$sleep$来让$cpu$的使用权交给其他程序
while (true) { try { thread.sleep(50); } catch (interruptedexception e) { e.printstacktrace(); }}
可以使用$wait$或者条件变量达到类似的效果
不同的是后两者都需要加锁,并且需要相应的唤醒操作,一般适用于要进行同步的场景
$sleep$适用于无需锁同步的场景
join方法以下程序的打印结果:
@slf4j(topic = "c.test6")public class test6 { static int r = 0; public static void main(string[] args) { test(); } private static void test() { log.debug("开始"); thread t = new thread("t1") { @override public void run() { log.debug("开始"); try { thread.sleep(1000); } catch (interruptedexception e) { e.printstacktrace(); } log.debug("结束"); r = 10; } }; t.start(); log.debug("r的值是{}", r); log.debug("结束"); }}
因为主线程和$t1$线程是并行的,$t1$线程需要$1s$后才能计算出$r$的值,而主线程一开始就要打印出$r$的值,因此打印的值为0
解决方法:
在$t.start();$后边加上$t.join();$即可。$join$的作用是等待某线程运行结束。
以调用方的角度来说,需要等待结果返回才能继续执行就是同步,不需要等待返回结果就能继续执行的就是异步。
因此$join$方法实际上是让其同步执行
有实效的等待$join(毫秒)$方法里可以有一个参数是传入等待的时间,如果线程执行时间大于等待时间,则等待时间到了之后,就会停止等待。如果线程执行时间小于等待时间,则线程执行完毕之后,等待也会跟着结束。不会把设置的等待时间过完。
interrupt方法打断$sleep, wait, join$的线程,即打断阻塞状态的线程
打断$sleep$的线程,会清空打断状态
@slf4j(topic = "c.test7")public class test7 { public static void main(string[] args) throws interruptedexception { thread t = new thread("t1"){ @override public void run() { log.debug("sleep..."); try { thread.sleep(5000); } catch (interruptedexception e) { e.printstacktrace(); } } }; t.start(); thread.sleep(1000); log.debug("interrupt"); t.interrupt(); log.debug("打断标记: {}", t.isinterrupted()); }}
打断正常运行的线程,不会清空打断状态因此我们可以在线程中判断打断标记,来决定是否被打断,以及执行被打断之前的收尾工作。
@slf4j(topic = "c.test8")public class test8 { public static void main(string[] args) throws interruptedexception { thread t = new thread("t1"){ @override public void run() { while (true) { if (thread.currentthread().isinterrupted()) { log.debug("线程被打断了"); break; } } } }; t.start(); thread.sleep(1000); log.debug("interrupt"); t.interrupt(); }}
守护线程默认情况下,$java$需要等待所有线程都运行结束,才会结束。有一种特殊的线程叫做守护线程,只要其他非守护线程运行结束了,即使守护线程的代码没有执行完毕,也会强制结束。
@slf4j(topic = "c.test10")public class test10 { public static void main(string[] args) throws interruptedexception { thread t = new thread("t1") { @override public void run() { while (true) { } } }; //设置线程为守护线程 t.setdaemon(true); t.start(); thread.sleep(1000); log.debug("主线程结束"); }}
如果不把$t$设置为守护线程,则因为线程内部的死循环,导致程序不会结束运行。
以上就是java线程中的start方法和run方法怎么使用的详细内容。
