-创建线程
线程的创建方式:
1.继承Thread类
public class ThreadCreateDemo1 {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
class MyThread extends Thread {
@Override
public void run() {
super.run();
System.out.println("hellow_world!");
}
}
2.实现Runnable接口
public class ThreadCreateDemo2 {
public static void main(String[] args) {
Runnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start();
}
}
class MyRunnable implements Runnable {
public void run() {
System.out.println("通过Runnable创建的线程!");
}
}
上述两种创建方式,工作时性质一样。但是建议使用实现Runable接口方式。解决单继承的局限性。
-线程运行结果与执行顺序无关
线程的调度是由CPU决定,CPU执行子任务时间具有不确定性。
public class ThreadRandomDemo1 {
public static void main(String[] args) {
Thread[] threads = new Thread[10];
for (int i = 0; i < 10; i++) {
threads[i] = new RandomThread("RandomThread:" + i);
}
for(Thread thread : threads) {
thread.start();
}
}
}
class RandomThread extends Thread {
public RandomThread(String name) {
super(name);
}
@Override
public void run() {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
以上10个线程,代码按照顺序执行,但是结果可以看出没有按照顺序执行,而且多次执行结果基本不同。
如图每次打印count都是正常递减,这里解释一下synchronized关键字,含有synchronized关键字的这个方法称为“互斥区” 或“临界区”,只有获得这个关键字对应的锁才能执行方法体,方法体执行完自动会释放锁。
-停止线程
终止正在运行的线程方法有三种:
1)使用退出标志,使线程正常的执行完run方法终止。
2)使用interrupt方法,使线程异常,线程进行捕获或抛异常,正常执行完run方法终止。
1.使用退出标志方法
public class ThreadVariableStopDemo {
public static void main(String[] args) throws InterruptedException {
VariableStopThread thread = new VariableStopThread("thread_1");
thread.start();
Thread.sleep(1);
thread.Stop();
}
}
class VariableStopThread extends Thread {
private boolean interrupt = true;
public VariableStopThread(String name) {
super(name);
}
public void run() {
System.out.println(Thread.currentThread().getName() + ":线程开始运行!");
int i = 0;
while(interrupt) {
System.out.println("" + (i++));
}
System.out.println("我停止了! timer:" + System.currentTimeMillis());
}
public void Stop() {
System.out.println(Thread.currentThread().getName() + ":线程设置了停止! timer:" + System.currentTimeMillis());
this.interrupt = false;
}
}
Thread_1中启动了一个while循环,一直打印i的累加值。main线程在sleep 1ms后设置Thread_1停止标志。Thread_1 while循环判断条件不符合正常执行完run方法结束。从中可以看出设置完停止标志后13还是正常打印,原因是因为while方法体中是原子操作,不能直接打断。
public class ThreadVariableStopDemo {
public static void main(String[] args) throws InterruptedException {
VariableStopThread thread = new VariableStopThread("thread_1");
thread.start();
Thread.sleep(10);
thread.interrupt();
}
}
class VariableStopThread extends Thread {
public VariableStopThread(String name) {
super(name);
}
public void run() {
System.out.println(Thread.currentThread().getName() + ":线程开始运行!");
while(!isInterrupted()) {
}
System.out.println("我停止了! timer:" + System.currentTimeMillis());
}
}
2.使用interrupt方法
public class ThreadInterruptDemo {
public static void main(String[] args) throws InterruptedException {
Thread thread = new InterruptThread("thread_1");
thread.start();
Thread.sleep(1);
System.out.println(thread.getName() + "线程设置:interrupt");
thread.interrupt();
}
}
class InterruptThread extends Thread {
public InterruptThread(String name) {
super(name);
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "线程开始!");
for(int i =0; i < 1000; i++) {
try {
Thread.sleep(0);
System.out.println("" + (i + 1));
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + "线程捕获异常,退出循环!");
break;
}
}
System.out.println(Thread.currentThread().getName() + "线程结束!");
}
}
从上图可以看出线程正常退出,但是发现一点循环结构体后面一句打印也打印了,解决这个问题的方案有两个:
1.异常法
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "线程开始!");
try {
for(int i = 0; i < 1000; i++) {
if(Thread.currentThread().interrupted()) {
System.out.println(Thread.currentThread().getName() + "线程停止状态!");
throw new InterruptedException();
}
Thread.sleep(0);
System.out.println("" + (i + 1));
}
System.out.println(Thread.currentThread().getName() + "线程结束!");
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + "线程捕获异常,退出循环!");
e.printStackTrace();
}
}
代码有两个关键点:
1)for循环外捕获异常【这是程序的关键点】
2)判断设置了interrupted标志则抛出异常。
2.return法
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "线程开始!");
try {
for(int i = 0; i < 1000; i++) {
Thread.sleep(0);
System.out.println("" + (i + 1));
}
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + "线程捕获异常,退出循环!");
e.printStackTrace();
return;
}
System.out.println(Thread.currentThread().getName() + "线程结束!");
}
这个方法相对简单,也比较常用。两种方法结果都一样直接退出不进行后续工作,两种方法依据功能需求选择。
上述两个方法sleep都是0,这里给大家看看沉睡中退出,有一个现象会发生。
从可以看出sleep使用interrupt()退出直接进入异常,而且interrupt标志位置为false。