1
有一辆班车除司机外只能承载80个人,假设前中后三个门都能上车,如果坐满则不能再上车。请用线程模拟上车过程并且在控制台打印出是从哪个车门上车以及剩下的座位数。
比如:前门上车—还剩n个座
package demo0908;
/**
* @creat 2020-09-08-21:56
*/
public class ByCar implements Runnable{
int count=80;
@Override
public void run() {
while (true){
synchronized (this) {
if (count>=1){
System.out.println(Thread.currentThread().getName() + "上车---还剩" + (--count) + "个座");
}
else{
break;
}
}
}
}
}
package demo0908;
/**
* @creat 2020-09-08-21:59
*/
public class TestCar {
public static void main(String[] args) {
ByCar car=new ByCar();
Thread thread1 = new Thread(car, "前门");
Thread thread2 = new Thread(car, "中门");
Thread thread3 = new Thread(car, "后门");
thread1.start();
thread2.start();
thread3.start();
}
}
2
同时开启3个线程,共同输出100-200之间所有数字,并且在输出奇数的时候将线程名称打印出来。
package demo0908;
/**
* @creat 2020-09-08-22:24
*/
public class PrintNum implements Runnable{
int num=100;
@Override
public void run() {
while (true){
synchronized (this){
if(num<=200){
if (num%2==1){
System.out.println(Thread.currentThread().getName()+"----"+num++);
}else {
System.out.println(num++);
}
}else{
break;
}
}
}
}
}
package demo0908;
/**
* @creat 2020-09-08-22:28
*/
public class TestPrint {
public static void main(String[] args) {
PrintNum printNum = new PrintNum();
Thread thread1 = new Thread(printNum, "线程1");
Thread thread2 = new Thread(printNum, "线程2");
Thread thread3 = new Thread(printNum, "线程3");
thread1.start();
thread2.start();
thread3.start();
}
}
3
生产者&消费者问题
有一家汉堡店举办吃汉堡比赛,决赛时有3个顾客来吃,3个厨师来做,一个服务员负责协调汉堡的数量。
为了避免浪费,制作好的汉堡被放进一个能装有10个汉堡的长条状容器中,按照先进先出的原则取汉堡。
如果容器被装满,则厨师停止做汉堡,如果顾客发现容器内的汉堡吃完了,就可以拍响容器上的闹铃, 提醒厨师再做几个汉堡出来。
此时服务员过来安抚顾客,让他等待。而一旦厨师的汉堡做出来,就会让服务员通知顾客,汉堡做好了, 让顾客继续过来取汉堡。
package demo0909;
/**
* @creat 2020-09-09-17:32
*/
public class Customer implements Runnable{
int i=1;
@Override
public void run() {
while (true){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (Server.server){
if (Server.num>0){
System.out.println(Thread.currentThread().getName()+"正在吃第"+i+"个");
i++;
Server.num--;
Server.server.notifyAll();
}else{
try {
Server.server.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
package demo0909;
/**
* @creat 2020-09-09-17:31
*/
public class Producer implements Runnable{
int i=1;
@Override
public void run() {
while (true){
synchronized (Server.server){
if (Server.num<10){
Server.num++;
System.out.println(Thread.currentThread().getName()+"正在制作第"+i+"个汉堡");
i++;
Server.server.notifyAll();
}else{
try {
Server.server.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
public class Server {
static int num=0;
static Server server=new Server();
public static void main(String[] args) {
Customer customer = new Customer();
Thread thread1 = new Thread(customer, "顾客1");
Thread thread2 = new Thread(customer, "顾客2");
Thread thread3 = new Thread(customer, "顾客3");
Producer producer = new Producer();
Thread thread4 = new Thread(producer, "厨师1");
Thread thread5 = new Thread(producer, "厨师2");
Thread thread6 = new Thread(producer, "厨师3");
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
}
}
4
订票问题
三个窗口同时出火车票,一共100张,卖完为止
package demo0909;
import java.util.concurrent.ForkJoinPool;
/**
* @creat 2020-09-09-17:21
*/
public class Ticket implements Runnable{
int num=100;
@Override
public void run() {
while (true){
synchronized (this){
if (num==0){
break;
}else {
System.out.println(Thread.currentThread().getName()+"正在出票,余票为:"+(--num));
}
}
}
}
}
package demo0909;
/**
* @creat 2020-09-09-17:21
* 模拟三个窗口出票
*/
public class Test2 {
public static void main(String[] args) {
Ticket ticket = new Ticket();
Thread thread1 = new Thread(ticket, "窗口1");
Thread thread2 = new Thread(ticket, "窗口2");
Thread thread3 = new Thread(ticket, "窗口3");
thread1.start();
thread2.start();
thread3.start();
}
}
5
主线程循环到10的时候,停止子线程。使用线程池
package demo0909;
/**
* @creat 2020-09-09-17:10
*/
public class A implements Runnable {
static boolean flag=false;
@Override
public void run() {
for (int i=0;i<20;i++){
System.out.println(Thread.currentThread().getName()+"----"+i);
if (flag){
break;
}
}
}
}
package demo0909;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @creat 2020-09-09-17:10
* 主线程循环到10的时候,停止子线程。
*/
public class Test1 {
public static void main(String[] args) {
ExecutorService es = Executors.newFixedThreadPool(6);
es.submit(new A());
for (int i=0;i<20;i++){
System.out.println("主线程----"+i);
if (i==10){
A.flag=true;
}
}
es.shutdown();
}
}
6
需求:线程 t1 循环输出数字 主线程控制t1的暂停和继续。
主线程使用键盘输入 输入一次 t1 暂停 输入两次 t1继续
package demo0910;
import java.util.Scanner;
/**
* @creat 2020-09-09-19:31
*/
public class Test1 {
static Test1 t=new Test1();
public static void main(String[] args) {
Thread thread = new Thread(new A());
thread.start();
Scanner scanner = new Scanner(System.in);
while (true){
scanner.nextLine();
A.flag=true;
scanner.nextLine();
synchronized (t){
t.notify();
}
A.flag=false;
}
}
}
package demo0910;
/**
* @creat 2020-09-09-19:32
*/
public class A implements Runnable {
static boolean flag=false;
@Override
public void run() {
while (true){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (Test1.t){
System.out.println("正在打印---");
if(flag){
try {
Test1.t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}