学习原:来自哔哩哔哩:狂神
1.学习 Runnable
2.问题
1.问题1
java代码:
package com.sunweihao.example;
//多个线程同时操作一个对象
//拍电影的例子,先到先拍片
public class TestThread4 implements Runnable {
//有十部电影
private int ticketNums=10;
@Override
public void run() {
while (true){
if (ticketNums<=0){
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"---->拍了第"+ticketNums--+"剧");
}
}
public static void main(String[] args) {
TestThread4 ticket=new TestThread4();
new Thread(ticket,"颇多野老师").start();
new Thread(ticket,"苍井空老湿").start();
new Thread(ticket,"老司机").start();
}
}
打印结果:
问题分析:
解决办法:
加锁
package com.sunweihao.example;
//多个线程同时操作一个对象
//拍电影的例子
public class TestThread4Improve implements Runnable {
//有十部电影
private int ticketNums=10;
@Override
public void run() {
play();
}
private synchronized void play() {
while (true){
if (ticketNums<=0){
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"---->拍了第"+ticketNums--+"剧");
}
}
public static void main(String[] args) {
TestThread4Improve ticket=new TestThread4Improve();
new Thread(ticket,"颇多野老师").start();
new Thread(ticket,"苍井空老湿").start();
new Thread(ticket,"老司机").start();
}
}
结果:
2.Thread下载
步骤:
1.导包
2.代码:下载文件
package com.sunweihao.example;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class TestThread2 extends Thread {
private String url;
private String name;
public TestThread2(String url,String name){
this.url=url;
this.name=name;
//hhh
}
@Override
public void run() {
WebDownloader webDownloader=new WebDownloader();
webDownloader.downloader(url,name);
System.out.println("下载了文件名为:"+name);
}
public static void main(String[] args) {
TestThread2 testThread1=new TestThread2("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3620297292,2452146774&fm=26&gp=0.jpg","苍井空老师");
TestThread2 testThread2=new TestThread2("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3620297292,2452146774&fm=26&gp=0.jpg","颇多野老师");
TestThread2 testThread3=new TestThread2("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3620297292,2452146774&fm=26&gp=0.jpg","孙伟豪老师");
testThread1.start();
testThread2.start();
testThread3.start();
}
}
class WebDownloader{
//下载方法
public void downloader(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO异常,download方法出现错误");
}
}
}
3.生产消费者模式
展示图
1.有个共同的操作
这里共同操作是TV
生产者:演员
class Player extends Thread{
TV tv;
public Player(TV tv){
this.tv=tv;
}
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 20; 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 < 20; i++) {
tv.watch();
}
}
}
他们共同看TV
class TV{
//演员表演,观众等待T
//观众观看,演员等待F
String voice;//表演的节目
boolean flag=true;
//表演
public synchronized void play(String voice){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}//休眠
if (!flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}//等待
}
System.out.println("演员表演了:"+voice);
//通知观众观看
this.notifyAll();//通知唤醒
this.voice=voice;
this.flag=!this.flag;
}
//观看
public synchronized void watch(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}//休眠
if (flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}//等待
}
System.out.println("观看了:"+voice);
//通知演员表演
this.notifyAll();
this.flag=!this.flag;
}
}
关键代码介绍
1.this.wait()会导致休眠
2.this.notifyAll();//通知其他线程唤醒
3.notify();//通知一条线程唤醒
全部代码
package com.sunweihao.gaoji;
//测试生产者消费者问题2,信号灯法,标志位解决
public class TestPc2 {
public static void main(String[] args) throws Exception {
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() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 20; 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 < 20; i++) {
tv.watch();
}
}
}
//产品-节目
class TV{
//演员表演,观众等待T
//观众观看,演员等待F
String voice;//表演的节目
boolean flag=true;
//表演
public synchronized void play(String voice){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}//休眠
if (!flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}//等待
}
System.out.println("演员表演了:"+voice);
//通知观众观看
this.notifyAll();//通知唤醒
this.voice=voice;
this.flag=!this.flag;
}
//观看
public synchronized void watch(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}//休眠
if (flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}//等待
}
System.out.println("观看了:"+voice);
//通知演员表演
this.notifyAll();
this.flag=!this.flag;
}
}