1. 终止线程
- 方式一:线程正常执行完毕。
- 方式二:外部干涉(加入标识)。
- 注:不要使用stop(),destory(),不安全。
public class TerminateThread implements Runnable {
private boolean flag = true;
@Override
public void run() {
int i = 0;
while (flag){
System.out.println("我是新建线程"+"===============>"+i++);
}
}
public void terminate(){
flag = false;
}
public static void main(String[] args) {
TerminateThread thread = new TerminateThread();
new Thread(thread).start();
for (int i = 0; i < 100; i++) {
if (i == 99){
thread.terminate();
}
System.out.println("我是main线程"+"--->"+i);
}
}
}
2. 线程阻塞(sleep方法)
需求:模拟三个黄牛抢票,让线程阻塞出现并发问题。
public class SleepThread implements Runnable{
public static int ticket = 99;
@Override
public void run() {
while (true){
if (ticket < 0){
break;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"买到了第"+ticket--+"张票");
}
}
public static void main(String[] args) {
SleepThread thread = new SleepThread();
new Thread(thread,"线程A").start();
new Thread(thread,"线程B").start();
new Thread(thread,"线程C").start();
}
}
3. 线程阻塞(join方法)
例子: main线程运行时,join其他线程,让main线程阻塞。
public class JoinDemo implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"--->"+i);
}
}
public static void main(String[] args) throws InterruptedException {
JoinDemo joinDemo = new JoinDemo();
Thread thread = new Thread(joinDemo,"我是线程A");
thread.start();
for (int i = 0; i < 100; i++) {
//main线程输出到20时,线程Ajoin,main线程阻塞
if (i == 20){
thread.join();
}
System.out.println("我是main线程"+"-->"+i);
}
}
}
4. 线程由运行状态->就绪状态(yield方法)
注意:
- 1.礼让线程,让当前执行的线程暂停
- 2.不是阻塞线程,而是让线程由运行状态转入就绪状态
- 3.让CPU调度器重新调度
public class YieldDemo implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-"+"start");
//礼让线程,进入就绪状态,调度器重新调度
Thread.yield();
System.out.println(Thread.currentThread().getName()+"-"+"end");
}
public static void main(String[] args) {
YieldDemo y = new YieldDemo();
new Thread(y,"A").start();
new Thread(y,"B").start();
}
}
5. 线程优先级
- 1.线程的优先级用数字表示,范围从1到10。
- 2.优先级的设定建议在start()调用前。
- 3.优先级只是意味着获得调度的概率低,并不是绝对先调用优先级高后调用优先级低的线程。
public class PriorityDemo implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+
Thread.currentThread().getPriority());
}
public static void main(String[] args) {
PriorityDemo p1 = new PriorityDemo();
Thread t1 = new Thread(p1,"线程A");
Thread t2 = new Thread(p1,"线程B");
Thread t3 = new Thread(p1,"线程C");
Thread t4 = new Thread(p1,"线程D");
t1.setPriority(10);
t2.setPriority(10);
t3.setPriority(1);
t4.setPriority(1);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
6. 守护线程
- 守护线程:是为用户线程服务的,jvm停止不用等待守护线程执行完毕
- 默认:用户线程jvm等待用户线程执行完毕才会停止
public class Daemon {
public static void main(String[] args) {
Man m = new Man();
God g = new God();
Thread threadMan = new Thread(m,"man");
Thread threadGod = new Thread(g,"god");
threadGod.setDaemon(true);
threadMan.start();
threadGod.start();
}
}
class Man implements Runnable{
@Override
public void run() {
for (int i = 0;i < 99;i++){
System.out.println(Thread.currentThread().getName()+"-->"+i);
}
}
}
class God implements Runnable{
@Override
public void run() {
while (true){
System.out.println(Thread.currentThread().getName());
}
}
}