Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java multithreaded wait () and notify ()

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use java multithreaded wait () and notify (). I hope you will get something after reading this article. Let's discuss it together.

Note: wait () and notify () should be used in synchronized package com.test;import java.util.ArrayList;public class ThreadWaitTeste {public static void main (String [] args) {ArrayList ar = new ArrayList (); Product p = new Product (ar); Consumer c = new Consumer (ar); Thread T1 = new Thread (p); Thread T2 = new Thread (c); t1.start (); t2.start ();} class Product implements Runnable {ArrayList array;public Product (ArrayList array) {this.array= array } public void run () {while (true) {synchronized (array) {if (this.array.size () = 5) {this.array.remove (1); System.out.println ("Consumer size:" + array.size ());} else {try {array.notifyAll ();} catch (Exception e) {/ / TODO Auto-generated catch blocke.printStackTrace () } after reading this article, I believe you have some understanding of "how to use java multithreaded wait () and notify ()". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report