线程的启动
启动线程的两种方式
- 继承Thread类
线程代码:
package com.sun.thread;
public class MyThread extends Thread {
@Override
public void run() {
super.run();
System.out.println("我是线程");
}
}
运行类代码:
package test;
public class TestMyThread {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
System.out.println("我是main函数线程");
}
结果:
我是main函数线程
我是线程
可能会问为什么main函数先执行完了呢,t.start()不是在上面吗?这是因为线程是个子任务,cpu以不确定的方式,随机的时间来调用线程中的run方法,所以出现这样的结果,这也说明在使用多线程技术时,代码的运行结果与代码的执行顺序是无关的。
- 实现Runnable接口
线程代码:
package com.sun.thread;
public class MyThreadRunnable implements Runnable {
@Override
public void run() {
System.out.println("我是线程");
}
}
运行类代码:
package test;
public class TestMyThreadRunnable {
public static void main(String[] args) {
MyThreadRunnable myThread = new MyThreadRunnable();
Thread thread = new Thread(myThread);
thread.start();
System.out.println("我是main函数线程");
}
使用继承Thread类创建线程最大的局限,就是java不支持多继承,所以为了多继承,完全可以实现Runnable接口的方式,这两种方法创建的线程工作性质是一样的
注:在Thread类中,在源码中可以发现,Thread类实现了Runnable接口。
public class Thread implements Runnable
暂停线程
在java多线程中,可以使用suspend来暂停线程,resume来恢复线程的执行
不推荐使用,因为它和stop方法一样,都是过期的方法,缺点如下:
独占
使用suspend暂停线程极易造成公共同步对象的独占,暂停线程的时候不会释放当前锁,造成其他线程一直等待。。。到天亮还等到下一个天亮。。等到天荒地老不同步
使用suspend和resume也容易出现因为线程的暂停而导致数据不同步的情况出现。
停止线程
停止线程的三种方式
- 使用退出标记,使线程正常退出
线程代码:
public class MyThread extends Thread {
boolean tag = true;
public void setTag(boolean tag) {
this.tag = tag;
}
@Override
public void run() {
int i=0;
super.run();
while (tag) {
i++;
System.out.println("i=" + i);
}
System.out.println("我是退出标记的停止状态!");
}
}
运行类代码:
public class Test {
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(1000);
thread.setTag(false);
} catch (InterruptedException e) {
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end!");
}
}
结果:
.
.
.
i=138254
i=138255
i=138256
end!
我是退出标记的停止状态!
- 使用interrupt方法中断线程
线程代码:
public class MyThread extends Thread {
@Override
public void run() {
super.run();
for (int i = 0; i < 500000; i++) {
if (this.interrupted()) {
System.out.println("我是interrupt退出的停止状态!");
break;
}
System.out.println("i=" + (i + 1));
}
}
}
运行类代码:
public class Test {
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!");
}
}
结果:
.
.
.
i=367894
i=367895
i=367896
end!
我是interrupt退出的停止状态!
- 使用stop方法强行终止线程,但是不推荐使用此方法,这是过期作废的方法,可能产生不可预料的结果
使用stop释放锁会给数据造成不一致性的结果,数据可能会造成破坏,最终导致程序流程错误。所以不要学这了,没用,反正都是过期的方法了。
线程的优先级
- 线程的优先级具有继承性,比如A线程的优先级是8,B继承A,那么B的默认优先级也就是8。
- 高优先级的线程总是大部分先执行完,但不代表高优先级的线程全部先执行完。
- 线程优先级与结果打印顺序无关,优先级具有不确定性和随机性。
线程代码:
public class ThreadA extends Thread {
private boolean tag = true;
private int count = 0;
public void setTag(boolean tag) {
this.tag = tag;
}
public int getCount() {
return count;
}
@Override
public void run() {
while (tag) {
count++;
}
}
}
public class ThreadB extends Thread {
private boolean tag = true;
private int count = 0;
public void setTag(boolean tag) {
this.tag = tag;
}
public int getCount() {
return count;
}
@Override
public void run() {
while (tag) {
count++;
}
}
}
运行类代码:
public class TestMyThread {
public static void main(String[] args) {
try {
ThreadA a = new ThreadA();
a.setPriority(Thread.MIN_PRIORITY);
a.start();
ThreadB b = new ThreadB();
b.setPriority(Thread.MAX_PRIORITY);
b.start();
Thread.sleep(1000);
a.setTag(false);
b.setTag(false);
System.out.println("a=" + a.getCount());
System.out.println("b=" + b.getCount());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
结果:
a=378911314
b=408845767
守护线程
守护线程:当进程中不存在非守护线程的时候,守护线程自动关闭。
通俗的来讲,守护线程的作用相当于陪伴,典型的例子就是GC(垃圾回收机制),有程序运行,就有GC
本例中,主线程存在了5秒,则守护线程就守护了5秒
线程代码:
public class MyThreadDaemon extends Thread {
@Override
public void run() {
int count=0;
while (true) {
try {
count++;
sleep(1000);
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
运行类代码:
public class TestMyThreadDaemon {
public static void main(String[] args) throws Exception {
MyThreadDaemon threadDaemon=new MyThreadDaemon();
threadDaemon.setDaemon(true);
threadDaemon.start();
Thread.sleep(5000);
}
}
结果:
1
2
3
4
此为读书笔记,权当加强记忆