这篇主要是介绍java API中Thread类的其他方法的作用
### Thread类
void interrupt() 中断线程。
当线程处于冻结状态时(即,wait()、sleep())执行方法受阻时,
调用该方法,会将处于冻结状态的线程回复到运行状态。会出现一个InterruptException异常
void setPriority() 设置线程的优先级。线程一共有10个优先级,最大的优先级为10,最小为1.
默认为5。
~~~
class InterRunnable implements Runnable{
private int x;
private boolean flag=true;
public synchronized void run(){
if(flag){
try{
wait(); //让线程处于等待状态
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName()+"....exception..");
flag = false;
}
System.out.println(Thread.currentThread().getName()+"....run.."+x++);
}
}
public void changeFlag(){
this.flag = false;
}
}
public class ThreadMethod{
public static void main(String args[]){
InterRunnable ir = new InterRunnable();
Thread t1 = new Thread(ir);
Thread t2 = new Thread(ir);
Thread t3 = new Thread(ir);
t1.setPriority(Thread.MAX_PRIORITY); //设置最大优先级
t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
t3.start();
for(int i=0;i<100;i++){
if(i==60){
t1.interrupt();
t2.interrupt();
t3.interrupt();
break; //当i=60时,就让程序结束
}
System.out.println(Thread.currentThread().getName()+"........"+i);
}
System.out.println("over");
}
}
~~~
void setDaemon(boolean on)**将该线程标记为守护线程或用户线程。
该方法又称设置线程为后台线程,当它所依附的前台线程消亡时,它也自动消亡。
~~~
class InterRunnable implements Runnable{
private int x;
private boolean flag=true;
public synchronized void run(){
while(flag)
System.out.println(Thread.currentThread().getName()+"....run.."+x++);
}
public void changeFlag(){
this.flag = false;
}
}
public class ThreadMethod{
public static void main(String args[]){
InterRunnable ir = new InterRunnable();
Thread t1 = new Thread(ir);
Thread t2 = new Thread(ir);
Thread t3 = new Thread(ir);
t1.setDaemon(true); //设置为守护线程
t2.setDaemon(true); //设置为守护线程
t3.setDaemon(true); //设置为守护线程
t1.start();
t2.start();
t3.start();
for(int i=0;i<100;i++){
if(i==60){
break; //当i=60时,就让程序结束
}
System.out.println(Thread.currentThread().getName()+"........"+i);
}
System.out.println("over");
}
}
~~~
void join()等待该线程终止
当线程A执行到了B线程的join方法时,A就会等待,等B执行完了之后,A才会执行。
join可以用来临时加入线程执行。
~~~
<span style="font-size:18px;">class InterRunnable implements Runnable{
private int x;
private boolean flag=true;
public void run(){
for(int i=0;i<50;i++)
System.out.println(Thread.currentThread().getName()+"....run.."+i++);
}
public void changeFlag(){
this.flag = false;
}
}
public class ThreadMethod{
public static void main(String args[])throws Exception{
InterRunnable ir = new InterRunnable();
Thread t1 = new Thread(ir);
Thread t3 = new Thread(ir);
t1.start();
t1.join();
t3.start();
for(int i=0;i<100;i++){
if(i==60){
break; //当i=60时,就让程序结束
}
System.out.println(Thread.currentThread().getName()+"........"+i);
}
System.out.println("over");
}
}</span>
~~~
static void yield()
暂停当前正在执行的线程对象,并执行其他线程。
String toString()
返回该线程的字符串表示形式,包括线程名称、优先级和线程组。
~~~
<span style="font-size:18px;">class InterRunnable implements Runnable{
private int x;
private boolean flag=true;
public void run(){
for(int i=0;i<50;i++)
System.out.println(Thread.currentThread().getName()+"....run.."+i++);
Thread.yield();
}
public void changeFlag(){
this.flag = false;
}
}
public class ThreadMethod{
public static void main(String args[])throws Exception{
InterRunnable ir = new InterRunnable();
Thread t1 = new Thread(ir);
Thread t3 = new Thread(ir);
t1.start();
t3.start();
}
}
</span>
~~~