一、线程创建
- 继承Thread类,重写run方法;
- 实现Runnable接口;
- 实现Callable接口;
class MyThread extends Thread {
@Override
public void run() {
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
}
}
class CallableImpl implements Callable<String> {
@Override
public String call() throws Exception {
}
}
public class test {
public static void main(String[] args) {
new MyThread().start();
new Thread(new MyRunnable()).start();
new Thread(new FutureTask<String>(new CallableImpl())).start();
}
}
区别:继承只有一次机会,Runnable实现不占继承机会且可以多实现,Callable有返回值。
二、线程生命周期
主要状态包括:创建、就绪、运行、阻塞、终止。
-
sleep()与wait():
sleep:当前线程睡眠;
wait: 访问当前对象的线程wait,前提是当前方法必须加锁,如果锁不住方法,那么调用的对象wait无从谈起。
wait的时候,锁被释放了,sleep的时候,锁一直被持有。
notify:叫醒当前wait在我这个对象上的线程。 -
join() 在A线程中调用B线程的join,意思是两个线程合并,A线程要等待B线程执行完才恢复执行。
-
yield()让出cpu,当前线程进入就绪队列等待调度。
-
getPriority()/setPriority():获取与设置线程优先级。
-
interrupt()中断, 配合是否被中断的方法一起使用,可以停止wait()方法和sleep()方法。
-
stop()非常粗暴,会强行把执行到一半的线程终止,不推荐使用,貌似已经废弃。
三、死锁
死锁是两个及以上的线程互相持有对方需要的锁,并且都互相等待对方释放锁;
package com.zht.thread;
public class DeadLock implements Runnable {
public int flag = 1;
static Object o1 = new Object(), o2 = new Object();
public void run() {
if (flag == 1) {
synchronized (o1) {
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (o2) {
System.out.println("1");
}
}
}
if (flag == 0) {
synchronized (o2) {
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (o1) {
System.out.println("0");
}
}
}
}
public static void main(String[] args) {
DeadLock td1 = new DeadLock();
DeadLock td2 = new DeadLock();
td1.flag = 1;
td2.flag = 0;
Thread t1 = new Thread(td1);
Thread t2 = new Thread(td2);
t1.start();
t2.start();
}
}
四、生产者消费者模型
这里使用wait和nofify来实现一个生产者消费者模型
package com.zht.thread;
public class ProducerConsumer {
public static void main(String[] args) {
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Consumer c = new Consumer(ss);
new Thread(p).start();
new Thread(p).start();
new Thread(p).start();
new Thread(c).start();
}
}
class WoTou {
int id;
WoTou(int id) {
this.id = id;
}
public String toString() {
return "WoTou : " + id;
}
}
class SyncStack {
int index = 0;
WoTou[] arrWT = new WoTou[6];
public synchronized void push(WoTou wt) {
while (index == arrWT.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
arrWT[index] = wt;
index++;
}
public synchronized WoTou pop() {
while (index == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
index--;
return arrWT[index];
}
}
class Producer implements Runnable {
SyncStack ss = null;
Producer(SyncStack ss) {
this.ss = ss;
}
public void run() {
for (int i = 0; i < 20; i++) {
WoTou wt = new WoTou(i);
ss.push(wt);
System.out.println("生产了:" + wt);
try {
Thread.sleep((int) (Math.random() * 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
SyncStack ss = null;
Consumer(SyncStack ss) {
this.ss = ss;
}
public void run() {
for (int i = 0; i < 20; i++) {
WoTou wt = ss.pop();
System.out.println("消费了:" + wt);
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
网友评论