此篇文章均为线程基础,并未深入剖析
实现:继承Thread类,重写run()方法,调用start开启线程
public class ThreadStudy extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("run方法正在执行" + i);
}
}
//测试
public static void main(String[] args) {
ThreadStudy threadStudy = new ThreadStudy();
//如果调用run方法,那么会按顺序执行,先输出100遍run方法正在执行,再输出main方法正在执行
//threadStudy.run();
//如果调用start方法,那么就会穿插执行,取决于cpu
threadStudy.start();
for (int i = 0; i < 1000; i++) {
System.out.println("main方法正在执行" + i);
}
}
}
实现:实现Runnable接口,采用Thread静态代理,调用start方法
public class Threadtest3 implements Runnable{
private int ticketNum = 10;
@Override
public void run() {
while(true) {
if (ticketNum <= 0) break;
try {
Thread.sleep(200l);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() +"--拿到了第" + ticketNum-- + "张票");
}
}
//测试
public static void main(String[] args) {
Threadtest3 threadtest3 = new Threadtest3();
//Thread静态代理
new Thread(threadtest3, "小明").start();
new Thread(threadtest3, "小杨").start();
new Thread(threadtest3, "小张").start();
}
}
1.实现Callable接口,需要返回值类型,返回值即创建按执行服务ExecutorService中submit的返回值
2.重写call()方法
3.创建目标对象
4.创建执行服务ExecutorService es = Executors.newFixedThreadPool(num);
5.提交执行Future result = es.submit(target);
6.获取结果Boolean flag = result.get();
7.关闭服务es.shutdownNow();
public class TestCallable implements Callable {
@Override
public Object call() throws Exception {
System.out.println("多线程之实现Callable创建线程");
return true;//这个的return值,就是下边ExecutorService的submit的方法的返回值
}
//测试
public static void main(String[] args) throws ExecutionException, InterruptedException {
//创建目标对象
TestCallable target = new TestCallable();
//创建执行任务
ExecutorService es = Executors.newFixedThreadPool(3);
//提交执行,泛型的类型根据call方法的返回值决定
Future<Boolean> result = es.submit(target);
//获取执行结果
Boolean flag = result.get();
System.out.println(flag);
//关闭服务
es.shutdownNow();
}
}
线程六个状态是Thread类中枚举创建的public enum State {…}
线程停止不建议使用stop(),destory()方法,建议使用线程标识停止
public class ThreadStop implements Runnable{
//1.线程体中定义线程体使用的标识
private boolean flag = true;
@Override
public void run() {
int i = 0;
//2.线程体使用该表示
while (flag) {
System.out.println("run..Thread正在运行中" + i++);
}
}
//3.对外提供方法改变标识
public void stop () {
this.flag = false;
}
public static void main(String[] args) {
ThreadStop threadStop = new ThreadStop();
new Thread(threadStop).start();
for (int i = 0; i < 10000; i++) {
System.out.println("main主线程目前跑到" + i);
if (i == 9000) {
threadStop.stop();
}
}
}
}
new Thread().sleep(1000);
Thread.yield();
//这个插队就会先打出
new Thread(()-> System.out.println("插队")).join();
//Thread类中
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;
public final void setPriority(int newPriority) {
ThreadGroup g;
checkAccess();
if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
throw new IllegalArgumentException();
}
if((g = getThreadGroup()) != null) {
if (newPriority > g.getMaxPriority()) {
newPriority = g.getMaxPriority();
}
setPriority0(priority = newPriority);
}
}
public class TestSynchronized{
public static void main(String[] args) {
BuyTicket person = new BuyTicket();
new Thread(person,"小明").start();
new Thread(person,"小张").start();
new Thread(person,"小李").start();
}
}
class BuyTicket implements Runnable {
//票
private int ticketNum = 10;
//线程外部停止标志
boolean flag = true;
@Override
public void run() {
//买票
while(flag) {
buy();
}
}
//增加同步
private synchronized void buy() {
if (ticketNum <= 0) {
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(Thread.currentThread().getName() + "拿到第" + ticketNum-- + "张票");
}
}
public class TestSynchronized{
public static void main(String[] args) {
BuyTicket person = new BuyTicket();
new Thread(person,"小明").start();
new Thread(person,"小张").start();
new Thread(person,"小李").start();
}
}
class BuyTicket implements Runnable {
//票
TicketNum ticketNum = new TicketNum(10);
//线程外部停止标志
boolean flag = true;
@Override
public void run() {
//买票
while(flag) {
buy();
}
}
//增加同步
private void buy() {
synchronized (ticketNum) {
if (ticketNum.ticketNum <= 0) {
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(Thread.currentThread().getName() + "拿到第" + ticketNum.ticketNum-- + "张票");
}
}
}
class TicketNum {
int ticketNum;
public TicketNum (int ticketNum) {
this.ticketNum = ticketNum;
}
}
public class TestLock {
public static void main(String[] args) {
TestLock2 lock = new TestLock2();
new Thread(lock).start();
new Thread(lock).start();
new Thread(lock).start();
}
}
class TestLock2 implements Runnable {
int ticketNum = 10;
//定义锁 可重入锁
ReentrantLock reentrantLock = new ReentrantLock();
@Override
public void run() {
while (true) {
//加锁
reentrantLock.lock();
try {
if (ticketNum > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ticketNum 剩余" + ticketNum--);
} else {
break;
}
} finally {
//解锁
reentrantLock.unlock();
}
}
}
}
public class DeadLock {
public static void main(String[] args) {
MakeUp girl1 = new MakeUp(0, "女孩1");
MakeUp girl2 = new MakeUp(2, "女孩2");
girl1.start();
girl2.start();
}
}
//口红
class Lipstick{
}
//镜子
class Mirror{
}
//化妆
class MakeUp extends Thread{
//需要的资源只有一份,用static来保证
static Lipstick lipstick = new Lipstick();
static Mirror mirror = new Mirror();
int choice;//选择
String girlName;
MakeUp(int choice, String girlName){
this.choice = choice;
this.girlName = girlName;
}
@Override
public void run() {
//化妆
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void makeup () throws InterruptedException {
if (choice == 0) {
synchronized (lipstick) {
System.out.println(this.girlName + "获得口红的锁");
Thread.sleep(1000);
//代码在这个位置就会造成死锁
// synchronized (mirror) {
// System.out.println(this.girlName + "获得了镜子的锁");
// }
}
//出了第一个锁的代码块就不会造成死锁
synchronized (mirror) {
System.out.println(this.girlName + "获得了镜子的锁");
}
} else {
synchronized (mirror) {
System.out.println(this.girlName + "获得镜子的锁");
Thread.sleep(1000);
//代码在这个位置就会造成死锁
// synchronized (lipstick) {
// System.out.println(this.girlName + "获得了口红的锁");
// }
}
//出了第一个锁的代码块就不会造成死锁
synchronized (lipstick) {
System.out.println(this.girlName + "获得了口红的锁");
}
}
}
}
生产者与消费者同时使用一个缓冲区,理想状态为,当缓冲区满时,生产者不可生产商品;当缓冲区为空时,消费者不可消费商品
生产者、消费者、缓冲区
方法
public class TestPc {
//需要一个容器
LinkedList<Integer> containerList = new LinkedList<Integer>();
//容器最大容量
private static final Integer MAX_CONTAINER = 2;
//生产者
class Producer implements Runnable {
//生产者放入产品 (容器中小于2时放入,放到2时线程等待,等待消费者消费)
@Override
public void run() {
synchronized (containerList) {
//如果容器满了,就需要等待消费者消费
if (containerList.size() == MAX_CONTAINER) {
System.out.println("容器已满,生产者" + Thread.currentThread().getName() + "已停止生产");
//生产线程等待
try {
containerList.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
//如果没满,那么生产
containerList.add(1);
System.out.println("生产者" + Thread.currentThread().getName() + "正在生产" + "容器大小为" + containerList.size());
containerList.notify();
}
}
}
}
//消费者
class Consumer implements Runnable {
//消费者,当容器中数量不为0时,则消费,否则消费线程停止
@Override
public void run() {
synchronized (containerList) {
//如果容器为0,停止消费
if (containerList.size() == 0) {
System.out.println("消费者" + Thread.currentThread().getName() + "停止消费,容器已空");
try {
containerList.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
containerList.removeFirst();
System.out.println("消费者" + Thread.currentThread().getName() + "开始消费,容器大小" + containerList.size());
containerList.notify();
}
}
}
}
public static void main(String[] args) {
TestPc testPc = new TestPc();
Producer producer = testPc.new Producer();
Consumer consumer = testPc.new Consumer();
for (int i = 0; i < 100; i++) {
new Thread(producer).start();
new Thread(consumer).start();
}
}
}
public class TestPC2 {
public static void main(String[] args) {
TV tv = new TV();
new Player(tv).start();
new Watcher(tv).start();
}
}
//生产者
class Player extends Thread{
TV tv;
public Player(TV tv){
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
if (i%2==0){
this.tv.play("节目");
}else {
this.tv.play("广告");
}
}
}
}
//消费者
class Watcher extends Thread{
TV tv;
public Watcher(TV tv){
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
tv.watch();
}
}
}
//电视
class TV{
boolean flag = true; // flag是信号灯,演员说话, 观众等待;观众观看 , 演员等待
//节目或广告
String voice;
//表演
public synchronized void play(String voice){
//演员等待
if (!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("表演了"+voice);
this.voice = voice;
//让观众观看
this.notify();
this.flag = !this.flag;
}
//观看
public synchronized void watch(){
//观众等待
if (flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("观众看到了: "+voice);
//通知演员表演
this.notify();
this.flag = !this.flag;
}
}
public class TestPool implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args) {
ExecutorService es = Executors.newFixedThreadPool(3);
es.execute(new TestPool());
es.execute(new TestPool());
es.execute(new TestPool());
es.shutdown();
//如果实现的是Callable接口,使用submit方法
// es.submit(new TestPool());
// es.submit(new TestPool());
// es.submit(new TestPool());
// es.shutdown();
}
}