上次学习到了如何停止线程。这次学习暂停线程,线程的优先级,什么是守护线程
首先了暂停线程
暂停的线程意味着此线程还可以恢复运行,在java多线程中,suspend()方法暂时线程,resume()恢复线程的执行
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());
}
}

但是suspend和resume方法使用不当的话,极其容易造成公共对象的独占,这会导致其他线程无法访问公共对象
还有一点,这两个方法极其容易因为线程的暂停而导致数据不同步。
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();
}
}

线程的优先级
线程可以划分优先级,优先级较高的线程得到CPU资源较多,也就CPU优先执行优先级较高的线程
在java中,线程优先级可以分为1到10.也就是越大,优先级越高.如果小于1或者大于10则会抛出异常。
JDK中使用3个常量来预置定义优先级的值,代码如下
public final static int MIN_PRIORITY=1;
public final static int NORM_PRIORITY=5;
public final static int MAX_PRIORITY=10;
这里有个误区。不是说优先级高的线程就一定先执行完,之后才到优先级低的线程。优先级高的线程只是CPU尽量将执行资源让给优先级比较高的线程。
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();
}
}
}

守护线程
java线程中有两种线程,一种是用户线程,另一种是守护线程
守护线程是一种特殊的线程。当线程中不存在非守护线程了,则守护线程自动销毁。
用我自己的话来讲就是,如果一个线程是守护线程,那么它必须等到其他非守护线程全部结束运行后,守护线程才能结束运行。守护线程最典型的应用就是GC(垃圾回收器)。
网友评论