-
互斥:同一时间,只能有一个线程访问数据 同步:一个线程做了一件事,它会用某种方式告诉其他线程我做完了 Java中,通过关键字synchronized实现互斥,它既可以出现在方法体之上,也可以出现在方法体内,也可以以块的形式出现查看全部
-
Thread常用方法查看全部
-
自己看图查看全部
-
本节 要点查看全部
-
争用条件:当多个线程同时共享访问同一数据(内存区域)时,每个线程都尝试操作该数据,从而导致数据被破坏(corrupted),这种现象称为争用条件。 每个线程在操作数据时,先会从内存中读取数值到自己的存储区域,然后再进行运算,最后把经过运算后的数据写回到内存。 若当线程1进行数据操作后,它从内存中读取了数据并进行运算,但还没有来得及将结果写回到内存,就失去cpu资源而阻塞;到线程2执行,线程2对数据进行了运算并写会到了内存中,这时内存的数据已经被修改。之后,线程1获得CPU资源,把之前运算好的数值写回到内存中,从而覆盖了线程2运算写回到内存的值。查看全部
-
ctrl + f 调出查找方法查看全部
-
调用join()方法暂停所有其他线程,调用该方法的线程执行完毕后再执行其他线程。查看全部
-
ctrl + 1 调出菜单查看全部
-
public void interrupt()<br> If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException. 被阻塞的线程因某些原因需要被唤醒,如外部发生了中断,它需要响应,这时就通过抛出异常的方式作出一些响应查看全部
-
Thread.yield()查看全部
-
Thread.currentThread().getName()获取当前线程名称 Thread.currentThread()当前线程查看全部
-
广为流传的错误停止线程的方法:interrupt() interrupt()--初衷并不是用于停止线程 在调用sleep()或者join()的时候,一旦其他线程调用interrupt(),它将会收到一个异常,这些被阻塞的线程因为某些原因需要被唤醒,比如外部发生了中断而需要响应,这时就采用抛出异常的方式来使其作出响应。总而言之,interrupt()方法并不能正确地停止进程 一个线程在阻塞状态下(例如sleep),此时interrupt的话,将会产生两个结果: 1、进程的interrupt状态被清除(cleard)而非被设置(set)。 2、sleep方法会抛出异常。查看全部
-
通过设置Boolean值去结束线程: 在Boolean设置为false后,可以保证线程内的循环得以全部执行完成,保证流程的完整性,这样在执行完任务后有充分的时间去做代码清理工作查看全部
-
stop()方法不是正确停止线程的方法,它使得线程戛然而止,不知道做了哪些工作,哪些没有做查看全部
-
public static void yield() Causes the calling Thread to yield execution time to another Thread that is ready to run. The actual scheduling is implementation-dependent. thread.yield(),让出处理器资源 public final void join()<br> throws InterruptedException<br> Blocks the current Thread (Thread.currentThread()) until the receiver finishes its execution and dies.<br> join方法阻塞了当前的线程,直到调用它的线程执行完为止查看全部
举报
0/150
提交
取消