第七章
多线程
模拟车站售票
package com.share.test_3_31;
public class CheZhan {
public int num = 100;
public int id = 1;
/**
* synchronized
* 1、修饰方法,比如mai-----mai方法同一时间仅能有一个线程使用这个方法。
* 其他线程如果想使用,在方法外等待当前线程使用完毕
*
* synchronized(Object){
* }//锁住特定对象----仅允许特定对象访问方法块
* 注:不要在run方法中使用锁synchronized
*/
public synchronized void mai(){
if (num>0) {
System.out.println(Thread.currentThread().getName()
+"卖了一张车票,车票id:"+(id++)+"还剩"+(--num)+"张车票");
}else{
System.out.println("车票已售完,停止售票");
MyThread.bool = false;
}
System.out.println("欢迎下次光临");
}
public void mai2(){//多个线程能同时调用这个方法
//其它线程在此等待
synchronized(this){//仅当前线程能访问
if (num>0) {
System.out.println(Thread.currentThread().getName()
+"卖了一张车票,车票id:"+(id++)+"还剩"+(--num)+"张车票");
}else{
System.out.println("车票已售完,停止售票");
MyThread.bool = false;
}
}
System.out.println("欢迎下次光临");
}
}
package com.share.test_3_31;
public class Main {
public static void main(String[] args) {
CheZhan cz = new CheZhan();
MyThread t1 = new MyThread("1号窗口",cz);
MyThread t2 = new MyThread("2号窗口",cz);
MyThread t3 = new MyThread("3号窗口",cz);
MyThread t4 = new MyThread("4号窗口",cz);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
package com.share.test_3_31;
public class MyThread extends Thread {
public CheZhan cz;
public static boolean bool = true;
public MyThread(String name,CheZhan cz){
super(name);
this.cz = cz;
}
@Override
public void run() {
while(bool){
cz.mai();
}
}
}
package com.share.test_3_31;
public class Game {
public Object key1;
public Object key2;
public Game(){
key1 = new Object();
key2 = new Object();
}
public void start(int n){
if (n >0 ) {
synchronized(key1){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("通道1:---------继续游戏");
synchronized(key2){
System.out.println(Thread.currentThread().getName()+"逃脱成功");
}
}
} else{
synchronized(key2){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("通道2:---------继续游戏");
synchronized(key1){
System.out.println(Thread.currentThread().getName()+"逃脱成功");
}
}
}
}
}
package com.share.test_3_31;
public class GameMain {
class User extends Thread{
public Game g;
public int n;
User(String name, Game g,int n){
super(name);
this.g = g;
this.n = n;
}
@Override
public void run() {
g.start(n);
}
}
public static void main(String[] args) {
Game g = new Game();
User u1 = new GameMain().new User("sb",g,1);
User u2 = new GameMain().new User("sb1",g,-1);
u1.start();
u2.start();
}
}
package com.share.test_3_31;
import java.util.Random;
public class Test {
/*
* 中午打饭: 1、食堂----将饭菜打包【生产爱心便当】、取出爱心便当 2、最多存放三个爱心便当 3、有三个爱心人士做爱心便当 4、有6个人取便当
*/
public static void main(String[] args) {
Test t = new Test();
Mess mess = t.new Mess();
People people1 = t.new People("1号工作人员",mess);
People people2 = t.new People("2号工作人员",mess);
People people3 = t.new People("3号工作人员",mess);
Student s1 = t.new Student(mess,"汪洪毅");
Student s2 = t.new Student(mess,"滕彩俊");
Student s3 = t.new Student(mess,"林杰");
Student s4 = t.new Student(mess,"谭小雨");
Student s5 = t.new Student(mess,"肖峰");
Student s6 = t.new Student(mess,"徐周");
people1.start();
people2.start();
people3.start();
s1.start();
s2.start();
s3.start();
s4.start();
s5.start();
s6.start();
//java程序里最多能同时跑多少个线程
}
/**
* 爱心午餐
*/
public class Lunch {
String food[] = { "凉拌豆腐干", "烧黄豆", "回锅肉", "麻婆豆腐", "清炒白菜" };
String first;// 第一个菜
String second;// 第二个菜
String third;// 第三个菜
public Lunch() {
// 初始化属性
first = food[new Random().nextInt(food.length)];
second = food[new Random().nextInt(food.length)];
third = food[new Random().nextInt(food.length)];
}
public String toString() {
return "【" + first + "+" + second + "+" + third + "】";
}
}
/**
* 食堂
*/
public class Mess {
Lunch box[] = new Lunch[3];
int index = 0;
public synchronized void put(Lunch wucan) {// 将制作好的爱心便当放到指定位置
while (index == box.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notify();
box[index] = wucan;
index++;
}
public synchronized Lunch get() {
while (index == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notify();
index--;
return box[index];
}// 从指定位置取爱心便当
}
/**
* 食堂的工作人员 负责生产爱心便当
*/
public class People extends Thread{
public Mess mess = null;
public People(String name,Mess mess){
super(name);
this.mess = mess;
}
@Override
public void run() {
while(true){
Lunch lunch = new Lunch();
mess.put(lunch);
System.out.println(Thread.currentThread().getName()+"制作了爱心便当:"+lunch);
try {
sleep(new Random().nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 消费者
*/
public class Student extends Thread{
public Mess mess = null;
public Student(Mess mess,String name){
super(name);
this.mess = mess;
}
@Override
public void run() {
Lunch lunch = mess.get();
System.out.println(Thread.currentThread().getName()+"拿了一份爱心便当"+lunch);
}
}
}
package com.share.test_3_31;
public class Test1 {
/**
* 中午打饭:
* 1、食堂---将饭菜打包【生产爱心便当】、取出爱心便当
* 2、最多存放三个爱心便当
* 3、有三个爱心人士最爱心便当
* 4、有6个人排序取便当
*/
public static void main(String[] args) {
ShiTang st = new ShiTang();
PersonFan pf = new PersonFan(st);
PersonEat pe = new PersonEat(st);
Thread t1 = new Thread(pe);
Thread t2 = new Thread(pf);
Thread t3 = new Thread(pf);
Thread t4 = new Thread(pf);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
//饭菜类
class FanCai{
int id;
FanCai(int id){
this.id = id;
}
public String toString(){
return "饭菜"+id;
}
}
//食堂
class ShiTang{
FanCai[] arrFC = new FanCai[2];
int index = 0;
public synchronized void push(FanCai fc){//做便当
System.out.println(index);
while(index == 2){
try {
this.wait();
System.out.println("饭菜已经做好");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.notifyAll();
arrFC[index] = fc;
index++;
}
public synchronized FanCai pop(){//取饭菜
while(index == 0){
try {
this.wait();
//System.out.println("吃饭了。。。");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.notifyAll();
index --;
return arrFC[index];
}
}
//打饭的人
class PersonFan implements Runnable{
ShiTang st = null;
PersonFan(ShiTang st){
this.st = st;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
FanCai fc = new FanCai(i);
st.push(fc);
System.out.println("生产了饭菜"+fc);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//吃饭的人
class PersonEat implements Runnable{
ShiTang st = null;
PersonEat(ShiTang st){
this.st = st;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
FanCai fc = st.pop();
System.out.println("吃了饭菜"+fc);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.share.test_3_31;
import java.util.Random;
public class ZuoYe3 {
/**
* 模拟考试。。。。考试时间60秒
* 有4个考生,试卷有30道题,考生的做题速度随机1-4秒做一道题
* 考试开始时,考生开始做题,考试时间到之后收卷,所有考生不能再答题
* 输出每一个考生的答题情况,列如:考生1:在规定时间完成了15题,完成度:50%
*
*/
public static int time = 100;//时间轴
public static boolean bool = true;//允许答题
public class Student extends Thread{
public Student(String name){
super(name);
}
@Override
public void run() {
int n = 0;
while (n<30 && bool) {
n++;
try {
Thread.sleep((new Random().nextInt(4)+1)*time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (!bool) {
System.out.println("时间到,"+Thread.currentThread().getName()+"完成了"+n+"道题,完成度:"+n*100/30.0+"%");
}else{
System.out.println(Thread.currentThread().getName()+"提前完成了考试");
}
}
}
public static void main(String[] args) {
Student s1 = new ZuoYe3().new Student("1号考生");
Student s2 = new ZuoYe3().new Student("2号考生");
Student s3 = new ZuoYe3().new Student("3号考生");
Student s4 = new ZuoYe3().new Student("4号考生");
System.out.println("考试开始:");
s1.start();
s2.start();
s3.start();
s4.start();
try {
Thread.sleep(60*time);//考试时间60秒。主线程休眠,控制bool的值确保考试时间为60秒
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bool=false;
}
}