java Thread 类的几种方法
生活随笔
收集整理的這篇文章主要介紹了
java Thread 类的几种方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
//后臺進程
/** 對于java來說只要還有一個前臺線程在運行,這個進程就不結(jié)束,如果一個進程* 只有后臺線程,這個進程就會結(jié)束*/
package xian_cheng;public class Example06 {public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("main線程是后臺線程嗎?"+Thread.currentThread().isDaemon());DamonThtead dt=new DamonThtead();//創(chuàng)建一個DamonThread對象dt;Thread t=new Thread(dt,"后臺線程");//創(chuàng)建線程t共享dt資源System.out.println("t線程是后臺線程嗎?"+t.isDaemon());//java默認(rèn)的是前臺進程/**注意要將某個線程設(shè)置為后臺線程,必須在該線程啟動之前*t.setDaemon(true);語句必須位于t.start之前*/t.setDaemon(true);//將線程t設(shè)置為后臺線程t.start(); //開啟線程for (int i = 0; i < 10; i++) {System.out.println(i);}}}
class DamonThtead implements Runnable{public void run(){while (true) {System.out.println(Thread.currentThread().getName()+"------is running");}}}//線程的優(yōu)先級
package xian_cheng;
import java.security.PublicKey;public class Example07 {public static void main(String[] args) {// TODO Auto-generated method stubThread minThread=new Thread(new MinPriority(),"優(yōu)先級較低的線程");Thread maxThread=new Thread(new MaxPriority(),"優(yōu)先級較gao的線程");minThread.setPriority(Thread.MIN_PRIORITY);maxThread.setPriority(10);maxThread.start();minThread.start();}}//定義類MaxPriority實現(xiàn)Runnable接口
class MaxPriority implements Runnable{public void run(){for (int i = 0; i < 10; i++) {System.out.println(Thread.currentThread().getName()+"正在輸出:"+i);}}
}
//定義類MinPriority實現(xiàn)Runnable接口
class MinPriority implements Runnable{public void run(){for (int i = 0; i < 10; i++) {System.out.println(Thread.currentThread().getName()+"正在輸出:"+i);}}
}/線程的休眠package xian_cheng;
/** 線程的休眠,它能使線程進入阻塞狀態(tài)* sleep方法聲明拋出InterruptedException異常* 當(dāng)線程調(diào)用sleep()方法后在指定的時間內(nèi)該線程是不會執(zhí)行的* sleep()是靜態(tài)方法只能控制當(dāng)前正在運行的線程* */
public class Example08 {public static void main(String[] args) throws InterruptedException {// TODO Auto-generated method stubThread thread=Thread.currentThread();System.out.println(thread.getName());new Thread(new SleepThread()).start();for (int i = 0; i < 10; i++) {if (i==5) {Thread.sleep(2000);}System.out.println("主線程正在輸出:"+i);Thread.sleep(500);}}}
class SleepThread implements Runnable{public void run(){for (int i = 0; i < 10; i++) {if (i==3) {try {Thread.sleep(2000);//當(dāng)前線程休眠2秒} catch (InterruptedException e) {// TODO: handle exceptione.printStackTrace();}}System.out.println("線程一正在輸出:"+i);try {Thread.sleep(2000);//當(dāng)前線程休眠2秒} catch (InterruptedException e) {// TODO: handle exceptione.printStackTrace();}}}
}
/線程讓步
package xian_cheng;
/** 線程讓步通過yield()方法來實現(xiàn),與sleep()的區(qū)別是它不會阻塞該線程,* 只是將線程轉(zhuǎn)換為就緒狀態(tài)*/
public class Example09 {public static void main(String[] args) {// TODO Auto-generated method stubThread t1=new YieldThread("線程A");Thread t2=new YieldThread("線程B");t1.start();t2.start();}}class YieldThread extends Thread{public YieldThread(String name){super(name);//調(diào)用父類的構(gòu)造方法}public void run(){for (int i = 0; i < 10; i++) {System.out.println(Thread.currentThread().getName()+"---"+i);if (i==3) {System.out.print("線程讓步:");Thread.yield();//線程做出讓步}}}}/線程同步
package xian_cheng;
/** 線程同步*/
public class Example12 {public static void main(String[] args) {// TODO Auto-generated method stubTicket1 ticket1=new Ticket1();new Thread(ticket1,"窗口1").start();new Thread(ticket1,"窗口2").start();new Thread(ticket1,"窗口3").start();}}class Ticket1 implements Runnable{private int tickets=10;Object look=new Object();//定義一個對象作為同步代碼塊的鎖/** 注意鎖對象的創(chuàng)建代碼不能放在run方法中,否者每個線程運行到run()方法都會* 創(chuàng)建一個鎖,這樣每個線程都有一個不同的鎖,每一個鎖都有一個自己的標(biāo)志位。* 線程之間便不能產(chǎn)生同步的效果*/public void run(){while (true) {synchronized (look) {try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO: handle exceptione.printStackTrace();}if (tickets>0) {System.out.println(Thread.currentThread().getName()+"---賣出的票"+tickets--);}else {break;}}}}
}
總結(jié)
以上是生活随笔為你收集整理的java Thread 类的几种方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java多线程------实现Runna
- 下一篇: java------线程同步方法