易语言多线程稳定写法,多线程处理工作的能力
终极管理员 知识笔记 565阅读
read t2 new Thread(my); //Thread(Runnable target, String name) Thread t1 new Thread(my,坦克); Thread t2 new Thread(my,飞机); //启动线程 t1.start(); t2.start(); }} 方式三实现Callable接口 方法介绍
public class MyCallable implements Callable<String> { Override public String call() throws Exception { for (int i 0; i < 100; i) { System.out.println(跟女孩表白 i); } //返回值就表示线程运行完毕之后的结果 return 答应; }}public class Demo { public static void main(String[] args) throws ExecutionException, InterruptedException { //线程开启之后需要执行里面的call方法 MyCallable mc new MyCallable(); //可以获取线程执行完毕之后的结果.也可以作为参数传递给Thread对象 FutureTask<String> ft new FutureTask<>(mc); //创建线程对象 Thread t1 new Thread(ft); String s ft.get(); //开启线程 t1.start(); //String s ft.get(); System.out.println(s); }}
三种多线程实现方法对比 public class MyThread extends Thread { public MyThread() {} public MyThread(String name) { super(name); } Override public void run() { for (int i 0; i < 100; i) { System.out.println(getName():i); } }}public class MyThreadDemo { public static void main(String[] args) { MyThread my1 new MyThread(); MyThread my2 new MyThread(); //void setName(String name)将此线程的名称更改为等于参数 name my1.setName(高铁); my2.setName(飞机); //Thread(String name) MyThread my1 new MyThread(高铁); MyThread my2 new MyThread(飞机); my1.start(); my2.start(); //static Thread currentThread() 返回对当前正在执行的线程对象的引用 System.out.println(Thread.currentThread().getName()); }}
线程休眠 public class MyRunnable implements Runnable { Override public void run() { for (int i 0; i < 100; i) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() --- i); } }}public class Demo { public static void main(String[] args) throws InterruptedException { /*System.out.println(睡觉前); Thread.sleep(3000); System.out.println(睡醒了);*/ MyRunnable mr new MyRunnable(); Thread t1 new Thread(mr); Thread t2 new Thread(mr); t1.start(); t2.start(); }}
线程优先级 线程调度方式

Java使用的是抢占式调度模型
优先级相关方法

public class MyCallable implements Callable<String> { Override public String call() throws Exception { for (int i 0; i < 100; i) { System.out.println(Thread.currentThread().getName() --- i); } return 线程执行完毕了; }}public class Demo { public static void main(String[] args) { //优先级: 1 - 10 默认值:5 MyCallable mc new MyCallable(); FutureTask<String> ft new FutureTask<>(mc); Thread t1 new Thread(ft); t1.setName(飞机); t1.setPriority(10); //System.out.println(t1.getPriority());//5 t1.start(); MyCallable mc2 new MyCallable(); FutureTask<String> ft2 new FutureTask<>(mc2); Thread t2 new Thread(ft2); t2.setName(坦克); t2.setPriority(1); //System.out.println(t2.getPriority());//5 t2.start(); }}
守护线程 当非守护线程结束之后守护线程也会相继结束
public class MyThread1 extends Thread { Override public void run() { for (int i 0; i < 10; i) { System.out.println(getName() --- i); } }}public class MyThread2 extends Thread { Override public void run() { for (int i 0; i < 100; i) { System.out.println(getName() --- i); } }}public class Demo { public static void main(String[] args) { MyThread1 t1 new MyThread1(); MyThread2 t2 new MyThread2(); t1.setName(女神); t2.setName(备胎); //把第二个线程设置为守护线程 //当普通线程执行完之后,那么守护线程也没有继续运行下去的必要了. t2.setDaemon(true); t1.start(); t2.start(); }}
礼让线程和插入线程 package com.itheima.a07threadmethod4;public class MyThread extends Thread{ Override public void run() {//飞机 坦克 for (int i 1; i < 100; i) { System.out.println(getName() i); //表示出让当前CPU的执行权 Thread.yield(); } }}
package com.itheima.a08threadmethod5;public class ThreadDemo { public static void main(String[] args) throws InterruptedException { MyThread t new MyThread(); t.setName(土豆); t.start(); //表示把t这个线程插入到当前线程之前。 //t:土豆 //当前线程: main线程 t.join(); //执行在main线程当中的 for (int i 0; i < 10; i) { System.out.println(main线程 i); } }}
线程生命周期 多线程–线程同步 示例卖票 案例需求
某电影院目前正在上映国产大片共有100张票而它有3个窗口卖票请设计一个程序模拟该电影院卖票实现步骤
定义一个类SellTicket实现Runnable接口里面定义一个成员变量private int tickets 100;
在SellTicket类中重写run()方法实现卖票代码步骤如下
判断票数大于0就卖票并告知是哪个窗口卖的
卖了票之后总票数要减1
票卖没了线程停止
定义一个测试类SellTicketDemo里面有main方法代码步骤如下
创建SellTicket类的对象
创建三个Thread类的对象把SellTicket对象作为构造方法的参数并给出对应的窗口名称
启动线程
public class SellTicket implements Runnable { private int tickets 100; //在SellTicket类中重写run()方法实现卖票代码步骤如下 Override public void run() { while (true) { if(ticket < 0){ //卖完了 break; }else{ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } ticket--; System.out.println(Thread.currentThread().getName() 在卖票,还剩下 ticket 张票); } } }}public class SellTicketDemo { public static void main(String[] args) { //创建SellTicket类的对象 SellTicket st new SellTicket(); //创建三个Thread类的对象把SellTicket对象作为构造方法的参数并给出对应的窗口名称 Thread t1 new Thread(st,窗口1); Thread t2 new Thread(st,窗口2); Thread t3 new Thread(st,窗口3); //启动线程 t1.start(); t2.start(); t3.start(); }}
问题发现 卖票出现了问题
相同的票出现了多次
出现了负数的票
问题产生原因
线程执行的随机性导致的,可能在卖票过程中丢失cpu的执行权,导致出现问题安全问题出现的条件
是多线程环境
有共享数据
有多条语句操作共享数据
如何解决多线程安全问题呢?
基本思想让程序没有安全问题的环境怎么实现呢?
把多条语句操作共享数据的代码给锁起来让任意时刻只能有一个线程执行即可
Java提供了同步代码块的方式来解决
问题解决1同步代码块synchronized(任意对象) { 多条语句操作共享数据的代码 }
同步的好处和弊端
好处解决了多线程的数据安全问题
弊端当线程很多时因为每个线程都会去判断同步上的锁这是很耗费资源的无形中会降低程序的运行效率
public class SellTicket implements Runnable { private int tickets 100; private Object obj new Object(); Override public void run() { while (true) { synchronized (obj) { // 对可能有安全问题的代码加锁,多个线程必须使用同一把锁 //t1进来后就会把这段代码给锁起来 if (tickets > 0) { try { Thread.sleep(100); //t1休息100毫秒 } catch (InterruptedException e) { e.printStackTrace(); } //窗口1正在出售第100张票 System.out.println(Thread.currentThread().getName() 正在出售第 tickets 张票); tickets--; //tickets 99; } } //t1出来了这段代码的锁就被释放了 } }}public class SellTicketDemo { public static void main(String[] args) { SellTicket st new SellTicket(); Thread t1 new Thread(st, 窗口1); Thread t2 new Thread(st, 窗口2); Thread t3 new Thread(st, 窗口3); t1.start(); t2.start(); t3.start(); }}
问题解决2同步方法 非静态同步方法
修饰符 synchronized 返回值类型 方法名(方法参数) { 方法体}
锁对象this 静态同步方法
修饰符 static synchronized 返回值类型 方法名(方法参数) { 方法体}
锁对象类名.class public class MyRunnable implements Runnable { private static int ticketCount 100; Override public void run() { while(true){ if(窗口一.equals(Thread.currentThread().getName())){ //同步方法 boolean result synchronizedMthod(); if(result){ break; } } if(窗口二.equals(Thread.currentThread().getName())){ //同步代码块 synchronized (MyRunnable.class){ if(ticketCount 0){ break; }else{ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } ticketCount--; System.out.println(Thread.currentThread().getName() 在卖票,还剩下 ticketCount 张票); } } } } } private static synchronized boolean synchronizedMthod() { if(ticketCount 0){ return true; }else{ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } ticketCount--; System.out.println(Thread.currentThread().getName() 在卖票,还剩下 ticketCount 张票); return false; } }}
问题解决3线程锁Lock public class Ticket implements Runnable { //票的数量 private int ticket 100; private Object obj new Object(); private ReentrantLock lock new ReentrantLock(); // 定义为静态变量 Override public void run() { while (true) { //synchronized (obj){//多个线程必须使用同一把锁. try { lock.lock(); if (ticket < 0) { //卖完了 break; } else { Thread.sleep(100); ticket--; System.out.println(Thread.currentThread().getName() 在卖票,还剩下 ticket 张票); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } // } } } }public class Demo { public static void main(String[] args) { Ticket ticket new Ticket(); Thread t1 new Thread(ticket); Thread t2 new Thread(ticket); Thread t3 new Thread(ticket); t1.setName(窗口一); t2.setName(窗口二); t3.setName(窗口三); t1.start(); t2.start(); t3.start(); }}
多线程应用–死锁【非概念是一种BUG现象】 线程死锁是指由于两个或者多个线程互相持有对方所需要的资源导致这些线程处于等待状态无法前往执行
什么情况下会产生死锁
资源有限
同步嵌套
package com.itheima.a12deadlock;public class ThreadDemo { public static void main(String[] args) { MyThread t1 new MyThread(); MyThread t2 new MyThread(); t1.setName(线程A); t2.setName(线程B); t1.start(); t2.start(); }}package com.itheima.a12deadlock;public class MyThread extends Thread { static Object objA new Object(); static Object objB new Object(); Override public void run() { //1.循环 while (true) { if (线程A.equals(getName())) { synchronized (objA) { System.out.println(线程A拿到了A锁准备拿B锁);//A synchronized (objB) { System.out.println(线程A拿到了B锁顺利执行完一轮); } } } else if (线程B.equals(getName())) { if (线程B.equals(getName())) { synchronized (objB) { System.out.println(线程B拿到了B锁准备拿A锁);//B synchronized (objA) { System.out.println(线程B拿到了A锁顺利执行完一轮); } } } } } }}
多线程应用–生产者消费者模式示例 等待唤醒机制 阻塞队列实现等待唤醒机制 多线程的6种状态 没有运行状态
运行状态只是方便理解暂空
标签: