在main方法中创建并启动两个线程。
第一个线程循环随机打印100以内的整数,直到第二个线程从键盘读取了“Q”命令。
代码:
import java.util.Scanner;
/**
*
在main方法中创建并启动两个线程。
第一个线程循环随机打印100以内的整数,直到第二个线程从键盘读取了“Q”命令。
* @author liyuting
*
*/
public class TestStop {
public static void main(String[] args) {
MyThread1 m1 = new MyThread1();
MyThread2 m2 = new MyThread2(m1);
m1.start();
m2.start();
}
}
class MyThread2 extends Thread{
Scanner input = new Scanner(System.in);
MyThread1 m ;
public MyThread2(MyThread1 m){
this.m=m;
}
@Override
public void run() {
while(true){
System.out.println("请输入:");
char key = input.next().toUpperCase().charAt(0);
if(key=='Q'){
//中断MyThread1(通过MyThread1对象调用setFlag方法)
m.setFlag(false);
//本身线程停止
break;
}
}
}
}
class MyThread1 extends Thread{
boolean flag =true;
public void run() {
while(flag){
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
int num = (int)(Math.random()*100);
System.out.println("随机数:"+num);
}
}
public void setFlag(boolean flag) {
this.flag = flag;
};
}
模拟一个人生产50个玩具,每200毫秒生产一个,当生产到第20个时加入每秒吃1个馒头,共吃完3个后在接着生产的多线程。
代码:
public class Test{
public static void main(String[] args) {
Person p = new Person();
Producer pro = new Producer(p );
pro.start();
EatBread eat = new EatBread(p);
eat.start();
}
}
class Person{
boolean flag = true;
//吃馒头
public synchronized void eat(){
if(!flag){
for(int i=1;i<=3;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("吃了第"+i+"个馒头");
}
this.notify();
}
}
//生产玩具
public synchronized void product(){
for(int i=1;i<=50;i++){
if(i==21){
flag = false;
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("生产了一个玩具,目前个数为:"+i);
}
}
}
class Producer extends Thread{
private Person p;
public Producer(Person p){
this.p=p;
}
@Override
public void run() {
p.product();
}
}
class EatBread extends Thread{
private Person p;
public EatBread(Person p){
this.p=p;
}
@Override
public void run() {
p.eat();
}
}
编写程序,在main方法中创建一个线程。线程每隔一定时间(200ms以内的随机时间)产生一个0-100之间的随机整数,打印后将该整数放到集合中;
共产生100个整数,全部产生后,睡眠30秒,然后将集合内容打印输出;
在main线程中,唤醒interrupt上述睡眠的线程,使其尽快打印集合内容。
代码:
public class test{
public static void main(String[] args) {
PrintNum p = new PrintNum();
p.start();
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
p.interrupt();
}
}
class PrintNum extends Thread{
@Override
public void run() {
List<Integer> list = new ArrayList<>();
for(int i=1;i<=100;i++){
try {
Thread.sleep((int)(Math.random()*200));
} catch (InterruptedException e) {
e.printStackTrace();
}
int num = (int)(Math.random()*101);
System.out.println(num);
list.add(num);
}
//睡眠30s
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
// e.printStackTrace();
//打印集合中的内容
System.out.println("-------------------");
for (Integer i : list) {
System.out.print(i+"\t");
}
}
}
}
案例:多个个生产者和多个消费者,要求商品库存为0-20.
代码:
public class TestCommunication2 {
public static void main(String[] args) {
Clerk2 c = new Clerk2();
Productor2 p1 = new Productor2(c,"张无忌");
p1.start();
Productor2 p2 = new Productor2(c,"令狐冲");
p2.start();
Consumer2 con1 = new Consumer2(c,"赵敏");
con1.start();
Consumer2 con2 = new Consumer2(c,"小昭");
con2.start();
Consumer2 con3 = new Consumer2(c,"依琳小师妹");
con3.start();
}
}
class Consumer2 extends Thread{
private Clerk2 c;
public Consumer2(Clerk2 c,String name){
super(name);
this.c=c;
}
@Override
public void run() {
while(true){
//消费产品(调用Clerk对象的get方法)
c.get();
}
}
}
class Productor2 extends Thread{
private Clerk2 c;
public Productor2(Clerk2 c,String name){
super(name);
this.c=c;
}
@Override
public void run() {
while(true){
//生产产品(调用Clerk对象的save方法)
c.save();
}
}
}
class Clerk2{
int count = 0;//产品数量
//生产
public synchronized void save(){//默认锁对象:this
while(count>=20){//库存已满
//等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"生产了一件产品,目前库存为:"+(++count));
this.notifyAll();//唤醒其他正在等待的所有线程
}
//消费
public synchronized void get(){
while(count<=0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"消费了一件产品,目前库存为:"+(--count));
this.notifyAll();//唤醒其他正在等待的所有线程
}
}