Java高并发-多线程基础
1.多線程基礎(chǔ)
中斷線程
public void Thread.interrupt() //中斷線程
public boolean Thread.isInterrupted() //判斷是否被中斷
public static boolean Thread.interrupted() //判斷是否被中斷,并清除當(dāng)前中斷中斷
線程的基本操作
中斷線程
public void run(){
???????? while(true){
?????????????????? Thread.yeild();
???????? }
}
t1.interrupt();
public void run(){
???????? while(true){
?????????????????? if(Thread.currentThread().isInterrupted()){
??????????????????????????? System.out.println("Interruted!");
?????????????????? ???????? break;
?????????????????? }
?????????????????? Thread.yield();
???????? }
}
Java線程中的Thread.yield( )方法,譯為線程讓步。顧名思義,就是說當(dāng)一個線程使用了這個方法之后,它就會把自己CPU執(zhí)行的時間讓掉,
讓自己或者其它的線程運行,注意是讓自己或者其他線程運行,并不是單純的讓給其他線程。
??? yield()的作用是讓步。它能讓當(dāng)前線程由“運行狀態(tài)”進入到“就緒狀態(tài)”,從而讓其它具有相同優(yōu)先級的等待線程獲取執(zhí)行權(quán);但是,并不能保證在當(dāng)前線程調(diào)用yield()之后,其它具有相同優(yōu)先級的線程就一定能獲得執(zhí)行權(quán);也有可能是當(dāng)前線程又進入到“運行狀態(tài)”繼續(xù)運行!
線程的基本操作--線程中斷
public static native void sleep(long mills) throws InterruptedException
public void run(){
???????? while(true){
?????????????????? if(Thread.currentThread().isInterrupted()){
??????????????????????????? System.out.println("Interrupted!");
??????????????????????????? break;
?????????????????? }
?????????????????? try {
??????????????????????????? Thread.sleep(2000);
?????????????????? } catch (InterruptedException e){
??????????????????????????? System.out.println("Interrupted When Sleep");
??????????????????????????? //設(shè)置中斷狀態(tài),拋出異常后會清除中斷標記位
??????????????????????????? Thread.currentThread().interrupt();
?????????????????? }
?????????????????? Thread.yield();
???????? }
}
掛起(suspend)和繼續(xù)執(zhí)行(resume)線程
-? suspend()不會釋放鎖
-? 如果加鎖發(fā)生在resume()之前,則死鎖發(fā)生
stop()、suspend()和resume()方法不推薦使用
?
等待線程結(jié)束(join)和謙讓(yeild)
public final void join() throws InterruptedException
public final synchronized void join(long millis) throws InterruptedException
public class JoinMain {
???????? public volatile static int i = 0;
???????? public static class AddThread extends Thread {
?????????????????? @Override
?????????????????? public void run(){
??????????????????????????? for(i = 0; i < 10000000; i++);
?????????????????? }
???????? }
???????? public static void main(String[] args) throws InterruptedException {
?????????????????? AddThread at = new AddThread();
?????????????????? at.start();
?????????????????? at.join();
?????????????????? System.out.println(i);
???????? }
}
join的本質(zhì)???????????????????????? 線程執(zhí)行完畢后
while(isAlive()){???????? ???????? 系統(tǒng)會調(diào)用
???????? wait(0);???????????? ???????? notifyAll()
}
不要在Thread實例上使用wait()和notify()方法
守護線程
在后臺默默地完成一些系統(tǒng)性的服務(wù),比如垃圾回收線程、JIT線程就可以理解為守護線程
當(dāng)一個Java應(yīng)用內(nèi),只有守護線程時,Java虛擬機就會自然退出
Thread t = new DaemonT();
t.setDaemon(true);
t.start();
線程優(yōu)先級
高優(yōu)先級的線程更容易在競爭中獲勝
基本的線程同步操作
synchronized
- 指定加鎖對象:對給定對象加鎖,進入同步代碼前要獲得給定對象的鎖。
- 直接作用于實例方法:相當(dāng)于對當(dāng)前實力加鎖,進入同步代碼前要獲得當(dāng)前實例的鎖。
-直接作用于靜態(tài)方法:相當(dāng)于對當(dāng)前類加鎖,進入同步代碼前要獲得當(dāng)前類的鎖。
Object.wait() Object.notify()
會釋放鎖
指定加鎖對象
public void run(){
???????? FOR(int j = 0; j < 100000000; j+++){
?????????????????? synchronized(instance){
??????????????????????????? i++;
?????????????????? }
???????? }
}
實例方法同步
public synchronized void increase(){
???????? i++;
}
靜態(tài)方法同步
public static synchronized void increase(){
???????? i++;
}
參考《實戰(zhàn)Java高并發(fā)程序設(shè)計》
總結(jié)
以上是生活随笔為你收集整理的Java高并发-多线程基础的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度地图1
- 下一篇: awt jtable 多线程加载图片_J