Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
2 Star 2 Fork 0

小米/rush-java

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
rush-java
/
code
/
src
/
WaitTest.java
rush-java
/
code
/
src
/
WaitTest.java
WaitTest.java 2.89 KB
Copy Edit Raw Blame History
小米 authored 2020年12月15日 19:31 +08:00 . 虚拟机知识点梳理
/**
* WaitTest
* <p>
* 欢迎跟我一起学习,微信(lvgocc)公众号搜索:星尘的一个朋友
*
* @author lvgorice@gmail.com
* @version 1.0
* @blog @see http://lvgo.org
* @CSDN @see https://blog.csdn.net/sinat_34344123
* @date 2020年12月15日
*/
public class WaitTest {
public static void main(String[] args) {
// 创建工厂类
Factory factory = new Factory();
// 生产者
Thread producer = new Thread(() -> {
try {
factory.put();
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "生产者1");
producer.start();
// 生产者
Thread producer2 = new Thread(() -> {
try {
factory.put();
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "生产者2");
producer2.start();
// 消费者
Thread consumer = new Thread(() -> {
try {
factory.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "消费者");
consumer.start();
}
}
class Factory {
private int[] items = new int[1]; // 数据存储容器(为了演示方便,设置容量最多存储 1 个元素)
private int size = 0; // 实际存储大小
/**
* 生产方法
*/
public synchronized void put() throws InterruptedException {
// 循环生产数据
do {
while (size == items.length) { // 注意不能是 if 判断
// 存储的容量已经满了,阻塞等待消费者消费之后唤醒
System.out.println(Thread.currentThread().getName() + " 进入阻塞");
this.wait();
System.out.println(Thread.currentThread().getName() + " 被唤醒");
}
System.out.println(Thread.currentThread().getName() + " 开始工作");
items[0] = 1; // 为了方便演示,设置固定值
size++;
System.out.println(Thread.currentThread().getName() + " 完成工作");
// 当生产队列有数据之后通知唤醒消费者
this.notify();
} while (true);
}
/**
* 消费方法
*/
public synchronized void take() throws InterruptedException {
// 循环消费数据
do {
while (size == 0) {
// 生产者没有数据,阻塞等待
System.out.println(Thread.currentThread().getName() + " 进入阻塞(消费者)");
this.wait();
System.out.println(Thread.currentThread().getName() + " 被唤醒(消费者)");
}
System.out.println("消费者工作~");
size--;
// 唤醒生产者可以添加生产了
this.notifyAll();
} while (true);
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

冲!Java!让我们一口气领先到底吧!
No labels
MIT
Use MIT
Cancel

Releases

No release

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mifengjun/rush-java.git
git@gitee.com:mifengjun/rush-java.git
mifengjun
rush-java
rush-java
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /