线程基本方法
CurrentThread()方法
Thread.current();
返回当前代码段,正在被哪个线程调用信息。
isAlive()方法:
MyThread thread = new MyThread();
thread.isAlive();
判断当前线程是否处于活动状态。
Sleep()方法:
Thread.sleep(5000);
在指定的毫秒数内让特定的线程休眠(暂停执行),这个特定的线程是指this.currentThread()返回的线程。
GetId():
获取线程的唯一标识。
MyThread thread = new MyThread();
thread.getId();
停止线程:
Interrupt()方法:调用interrupt方法仅仅是在当前线程中打了一个停止的标记,并不是真的停止线程。
This. Interrupted():测试当前线程是否已经中断。
This. isInterrupted():测试线程是否已经中断。
两者的却别:前者:测试当前线程是否已经是中断状态,执行后具有将状态标志置清除为false的功能。
后者:测试线程对象是否已经是中断状态,但是不清除状态标志。
1、停止线程—异常法:
a.在main线程,对特定线程执行Interrupt;
b.在该线程中判断,This. Interrupted().返回true,则退出循环,并抛出异常,则退出线程:
public class MyThread extends Thread {
@Override
public void run() {
super.run();
try {
for (int i = 0; i < 500000; i++) {
if (this.interrupted()) {
System.out.println("11");
throw new InterruptedException();
}
System.out.println("i=" + (i + 1));
}
System.out.println("after 11");
} catch (InterruptedException e) {
System.out.println("11");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(2000);
thread.interrupt();
} catch (InterruptedException e) {
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end!");
}
}
2、stop()方法:
暴力停止线程,现已过时。使用stop()释放锁将会给数据造成不一致性的结果,程序数据有可能遭到破坏。
3、使用return停止线程:
a.在main中使用interrupt;
b.在该线程中检测this.isInterrupted,如果为true,则return,即可退出线程。
public class MyThread extends Thread {
@Override
public void run() {
while (true) {
if (this.isInterrupted()) {
System.out.println("11");
return;
}
System.out.println("timer=" + System.currentTimeMillis());
}
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
MyThread t=new MyThread();
t.start();
Thread.sleep(2000);
t.interrupt();
}
}
Suspend()方法:暂停线程;resume()方法:恢复线程
public class MyThread extends Thread {
private long i = 0;
public long getI() {
return i;
}
public void setI(long i) {
this.i = i;
}
@Override
public void run() {
while (true) {
i++;
}
}
}
import mythread.MyThread;
public class Run {
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(5000);
// A
thread.suspend();
System.out.println("A= " + System.currentTimeMillis() + " i="
+ thread.getI());
Thread.sleep(5000);
System.out.println("A= " + System.currentTimeMillis() + " i="
+ thread.getI());
// B
thread.resume();
Thread.sleep(5000);
// C
thread.suspend();
System.out.println("B= " + System.currentTimeMillis() + " i="
+ thread.getI());
Thread.sleep(5000);
System.out.println("B= " + System.currentTimeMillis() + " i="
+ thread.getI());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Yield()方法:
放弃当前CPU资源,将它让给其他任务去占用CPU执行时间,但是放弃的时间不确定,有可能是刚刚放弃,马上又获取CPU时间片。
线程优先级
在操作系统中,线程可以划分优先级,优先级较高的线程得到的CPU资源较多,也就是CPU优先执行优先级较高的线程对象。
设置线程优先级使用setPriority()方法。
优先级分为1-10这10个等级,如果设置小于1或者大于10,则会抛出错误。
优先级高的线程总是大部分先执行完,但是不代表高优秀级就一定能全部先执行完。并且代码执行结束时间和调用顺序无关。看以下代码:
public class MyThread1 extends Thread {
@Override
public void run() {
long beginTime = System.currentTimeMillis();
long addResult = 0;
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 50000; i++) {
Random random = new Random();
random.nextInt();
addResult = addResult + i;
}
}
long endTime = System.currentTimeMillis();
System.out.println("11111111thread 1 use time=" + (endTime - beginTime));
}
}
import java.util.Random;
public class MyThread2 extends Thread {
@Override
public void run() {
long beginTime = System.currentTimeMillis();
long addResult = 0;
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 50000; i++) {
Random random = new Random();
random.nextInt();
addResult = addResult + i;
}
}
long endTime = System.currentTimeMillis();
System.out.println("2222222thread 2 use time=" + (endTime - beginTime));
}
}
import extthread.MyThread1;
import extthread.MyThread2;
public class Run {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
MyThread1 thread1 = new MyThread1();
thread1.setPriority(1);
thread1.start();
MyThread2 thread2 = new MyThread2();
thread2.setPriority(10);
thread2.start();
}
}
}
从上述代码中的执行结果中,也能说明,线程的优先级具有随机性,也就是优先级高的线程,不一定每次都先执行完。
守护线程
Java线程分两种,一种是用户线程,一种是守护线程。
守护线程是一种特殊的线程,当进程中不存在非守护线程了,则守护线程自动销毁。典型的例子就是垃圾回收线程。在JVM中有任何一个非守护线程没有结束,守护线程就在工作,当最后一个非守护线程结束时,守护线程才会随着JVM一起结束工作。