上次學(xué)習(xí)到了如何停止線程。這次學(xué)習(xí)暫停線程,線程的優(yōu)先級,什么是守護(hù)線程
首先了暫停線程
暫停的線程意味著此線程還可以恢復(fù)運行,在java多線程中,suspend()方法暫時線程,resume()恢復(fù)線程的執(zhí)行
package learnThread;
public class myThread extends Thread {
private long i=0;
public long getI()
{
return i;
}
public void setI(long i)
{
this.i=i;
}
public void run()
{
while(true)
{
i++;
}
}
public static void main(String[] args) throws InterruptedException
{
myThread thread=new myThread();
thread.start();
Thread.sleep(5000);
thread.suspend();
System.out.println("A="+System.currentTimeMillis()+" i="+thread.getI());
Thread.sleep(5000);
System.out.println("A="+System.currentTimeMillis()+" i="+thread.getI());
thread.resume();
Thread.sleep(5000);
thread.suspend();
System.out.println("B="+System.currentTimeMillis()+" i="+thread.getI());
Thread.sleep(5000);
System.out.println("B="+System.currentTimeMillis()+" i="+thread.getI());
}
}

搜狗截圖18年04月04日2210_1.png
但是suspend和resume方法使用不當(dāng)?shù)脑挘瑯O其容易造成公共對象的獨占,這會導(dǎo)致其他線程無法訪問公共對象
還有一點,這兩個方法極其容易因為線程的暫停而導(dǎo)致數(shù)據(jù)不同步。
package jianshu;
public class MyObject
{
private String username="1";
private String password="11";
public void setValue(String u,String p)
{
this.username=u;
if(Thread.currentThread().getName().equals("a"))
{
System.out.println("停止a線程");
Thread.currentThread().suspend();
}
}
public void show()
{
System.out.println(username+" "+password);
}
}
package jianshu;
public class Run {
public static void main(String[] args) throws InterruptedException
{
final MyObject myObject=new MyObject();
Thread thread1=new Thread(){
public void run()
{
myObject.setValue("a", "aa");
}
};
thread1.setName("a");
thread1.start();
Thread.sleep(500);
Thread thread2=new Thread() {
public void run()
{
myObject.show();
}
};
thread2.start();
}
}

搜狗截圖18年04月04日2226_2.png
線程的優(yōu)先級
線程可以劃分優(yōu)先級,優(yōu)先級較高的線程得到CPU資源較多,也就CPU優(yōu)先執(zhí)行優(yōu)先級較高的線程
在java中,線程優(yōu)先級可以分為1到10.也就是越大,優(yōu)先級越高.如果小于1或者大于10則會拋出異常。
JDK中使用3個常量來預(yù)置定義優(yōu)先級的值,代碼如下
public final static int MIN_PRIORITY=1;
public final static int NORM_PRIORITY=5;
public final static int MAX_PRIORITY=10;
這里有個誤區(qū)。不是說優(yōu)先級高的線程就一定先執(zhí)行完,之后才到優(yōu)先級低的線程。優(yōu)先級高的線程只是CPU盡量將執(zhí)行資源讓給優(yōu)先級比較高的線程。
package jianshu;
import java.util.Random;
public class MyThread1 extends Thread
{
public void run()
{
long beginTime=System.currentTimeMillis();
long addResult=0;
for(int j=0;j<10;j++)
{
for(int i=0;i<10000;i++)
{
Random random=new Random();
random.nextInt();
addResult++;
}
}
long endtime=System.currentTimeMillis();
System.out.println("thread 1 use time="+(endtime-beginTime));
}
}
package jianshu;
import java.util.Random;
public class MyThread2 extends Thread
{
public void run()
{
long beginTime=System.currentTimeMillis();
long addResult=0;
for(int j=0;j<10;j++)
{
for(int i=0;i<10000;i++)
{
Random random=new Random();
random.nextInt();
addResult++;
}
}
long endtime=System.currentTimeMillis();
System.out.println("thread 2 use time="+(endtime-beginTime));
}
}
package jianshu;
public class Run1 {
public static void main(String[] args)
{
for(int i=0;i<5;i++)
{
MyThread1 thread1=new MyThread1();
thread1.setPriority(10);
thread1.start();
MyThread2 thread2=new MyThread2();
thread2.setPriority(1);
thread2.start();
}
}
}

搜狗截圖18年04月05日2050_1.png
守護(hù)線程
java線程中有兩種線程,一種是用戶線程,另一種是守護(hù)線程
守護(hù)線程是一種特殊的線程。當(dāng)線程中不存在非守護(hù)線程了,則守護(hù)線程自動銷毀。
用我自己的話來講就是,如果一個線程是守護(hù)線程,那么它必須等到其他非守護(hù)線程全部結(jié)束運行后,守護(hù)線程才能結(jié)束運行。守護(hù)線程最典型的應(yīng)用就是GC(垃圾回收器)。