内容
1.建立線程的另外一種方法以及Thread中幾個方法介紹
2.線程的優先級
3.sleep方法
一.建立線程的另外一種方法以及Thread中幾個方法介紹
1.另外一種方法?
實作Runnable接口
這個時候建立線程的代碼就要這樣寫Thread t1 = new Thread(new Processor());
而上一個方法直接是Thread t1 = new Processor()
2.Thread中的幾個常用方法
①擷取目前線程引用
②擷取目前線程的名字
③更改線程的名字
3.使用示例
public class 測試程式 {
public static void main(String[] args) {
//1.擷取目前線程對象
Thread t = Thread.currentThread();//t儲存的記憶體位址指向的線程是“主線程對象”
//2.擷取線程的名字
System.out.println(t.getName());//輸出main
//3.建立線程
Thread t1 = new Thread(new Processor());//如果線程使用的繼承,那麼可以直接寫Thread t1 = new Processor();注意兩種差別
//4.擷取線程的名字
System.out.println(t1.getName());
//5.給線程起名
t1.setName("t1");
t1.start();
}
}
//使用實作Runnable接口的方式建立線程
class Processor implements Runnable
{
public void run() {
Thread t = Thread.currentThread();//t儲存的記憶體位址指向的線程是“t1線程對象”
System.out.println(t.getName());
}
}
輸出結果:
main
Thread-0
t1
二.線程的優先級
1.優先級高低有什麼影響?
優先級高的線程擷取的CPU時間片相對多一些
2.優先級範圍
最低1
最高10
預設5
3.關于優先級的一些方法
①得到優先級
②設定優先級
4.使用示例
public class 測試程式 {
public static void main(String[] args) {
//1.列印幾個資料
System.out.println(Thread.MAX_PRIORITY);//10
System.out.println(Thread.MIN_PRIORITY);//1
System.out.println(Thread.NORM_PRIORITY);//5
//2.建立線程
Thread t1 = new Processor();
t1.setName("t1");
Thread t2 = new Processor();
t2.setName("t2");
//3.列印線程的優先級
System.out.println(t1.getPriority());
System.out.println(t2.getPriority());
//4.設定優先級
t1.setPriority(1);
t2.setPriority(10);
//5.啟動線程
t1.start();
t2.start();
}
}
class Processor extends Thread
{
public void run() {
Thread t = Thread.currentThread();
System.out.println(t.getName());
}
}
輸出結果
10
1
5
5
5
t2
t1或者是
10
1
5
5
5
t1
t2
三.sleep方法
1.sleep方法簡介
sleep方法是一個靜态方法(最好使用 類名點 的方式調用)
2.使用形式
Thread.sleep(毫秒);
3.方法作用:
阻塞目前線程,騰出CPU,讓給其他線程。
4.使用示例
public class 測試程式 {
public static void main(String[] args) {
Thread t1 = new Processor();
t1.setName("t1");
t1.start();
}
}
class Processor extends Thread
{
//Thread中的run方法不抛出異常,是以重寫run方法之後,在run方法的生命位置上不能使用throws
//是以run方法中的異常隻能try...catch...
public void run() {
for(int i = 0;i < 10;i++) {
System.out.println(Thread.currentThread().getName() + "--->" + i);
//使用trycatch
try {
Thread.sleep(1000);//阻塞1s鐘
}catch(InterruptedException e) {
e.printStackTrace();
}
//m1();
}
}
//m1方法可以使用throws
/*public void m1() throws Exception{
}*/
}
結果:
t1--->0
t1--->1
t1--->2
t1--->3
t1--->4
t1--->5
t1--->6
t1--->7
t1--->8
t1--->9每一秒輸出一行
5.面試題
問:t.sleep(1000)是否能使t線程休眠1s?
public class 測試程式 {
public static void main(String[] args) throws InterruptedException {
//1.建立線程
Thread t = new Processor();
t.setName("t");
//2.啟動線程
t.start();
//3.休眠
t.sleep(1000);
//由于sleep是靜态方法,是以等同于Thread.sleep(1000)
//也就是說阻塞的是目前線程,和t線程無關
System.out.println("helloworld!");
//這個helloworld會在main阻塞1s後輸出
}
}
class Processor extends Thread
{
public void run() {
for(int i = 0;i < 10;i++) {
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
6.打斷sleep
public class 測試程式 {
public static void main(String[] args) throws InterruptedException {
//需求:啟動線程,5s之後打斷線程的休眠
//1.建立線程
Thread t = new Thread(new Processor());
//2.起名
t.setName("t");
//3.啟動線程
t.start();
//4.5s之後
Thread.sleep(5000);
//5.打斷t的休眠
t.interrupt();
}
}
class Processor implements Runnable
{
public void run() {
try {
Thread.sleep(10000000L);//打斷之後就發生異常
//也就是說這個interrupt是靠的異常處理機制來實作的
System.out.println("Helloworld!");//這句話不會執行
}catch(InterruptedException e) {
e.printStackTrace();
}
for(int i = 0;i < 5;i++) {
System.out.println(i);
}
}
}
輸出結果
java.lang.InterruptedException: sleep interrupted
0
1
2
3
4
at java.lang.Thread.sleep(Native Method)
at 對象.Processor.run(測試程式.java:40)
at java.lang.Thread.run(Unknown Source)
如果是把e.printStackTrace();注釋掉
catch(InterruptedException e) {
//e.printStackTrace();
}
0
1
2
3
4
7.正确的終止程式
public class 測試程式 {
public static void main(String[] args) throws InterruptedException {
Processor p = new Processor();
Thread t = new Thread(p);
t.setName("t");
t.start();
//5s之後終止
Thread.sleep(5000);
//終止
p.run = false;
}
}
class Processor implements Runnable
{
boolean run = true;
public void run() {
for(int i = 0;i < 10;i++) {
if(run) {
try {
Thread.sleep(1000);
}catch(Exception e) {
e.printStackTrace();
}
System.out.println(i);
}else {
return;
}
}
}
}