java多线程(异步堆栈)

输出由两个生产者随机生成字母存储在异步堆栈中,两个消费者从异步堆栈中获取字符:

首先,Java把内存分为两种:一种是栈内存,一种是堆内存

栈内存:在函数中定义的一些基本类型的变量和对象的引用变量,当超过变量的作用域之后,Java自动释放该变量内存

堆内存:存放new创建的对象和数组,由JVM的GC(Java虚拟机的自动垃圾回收器)管理。

我把异步理解成“步速不同”也就是线程的先后不同。

MyStack代码

package edu.ccut.pr;

import java.util.LinkedList;

public class MyStack<T> {
	private LinkedList<T> list;
	private int count;

	public MyStack() {
		list = new LinkedList<>();
		count = 0;
	}

	public synchronized int getSize() {
		return count;
	}

	// 压栈
	public synchronized void push(T ele) {

		while (count >= 200) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		list.addLast(ele);
		count++;

		this.notifyAll();
	}

	// 出栈
	public synchronized T pull() {

		while (count <= 0) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}

		T res = list.pollLast();
		count--;

		this.notifyAll();
		return res;
	}

	// 查看最后的数据
	public synchronized T peek() {
		return list.peekLast();
	}

}

生产者Producer代码

package edu.ccut.pr;

import java.util.Random;

public class Producer implements Runnable{

	private MyStack<Character> stack;
	
	public Producer(MyStack<Character> stack) {
		this.stack = stack;
	}
	
	public void run() {
		while(true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			char c = generateChar();
			stack.push(c);
			System.out.println(Thread.currentThread().getName() + " : " + c);
		}
	}

	private char generateChar() {
		return (char) (new Random().nextInt(26) + 'A');//随机生成英文字母
	}
}

 消费者Consumer代码

package edu.ccut.pr;

public class Consumer implements Runnable{

	private MyStack<Character> stack;
	
	public Consumer(MyStack<Character> stack) { 
		this.stack = stack;
	}
	
	public void run() {
		while(true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println( Thread.currentThread().getName() + " : " + stack.pull());
		}
	}
	
}

 main主函数

package edu.ccut.pr;


public class ProducerConsumer {
public static void main(String[] args) {
		
		MyStack<Character> stack = new MyStack<>();
		Runnable runnable = new Producer(stack);
		Runnable runnable1 = new Consumer(stack);
		for (int i = 0; i < 2; i++) {
			Thread t1 = new Thread(runnable,"Producer1");
			t1.start();
			Thread t2 = new Thread(runnable,"Producer2");
			t2.start();
			try {
				t2.join(15);
			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			Thread t3 = new Thread(runnable1,"Consumer1");
			t3.start();
			try {
				t3.join(20);
			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			Thread t4 = new Thread(runnable1,"Consumer2");
			t4.start();
		}
		
	}

}

运行结果

  • 14
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

forget hurt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值