In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what are the application scenarios of Java threads". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "what are the application scenarios of Java threads".
To solve the problems of producers and consumers, you can use the following methods (code examples include):
1. Solve the data synchronization problem: using synchronization code blocks
two。 Resolve repetitive issues:
Add wait and wake-up mechanism-- > methods provided by the Object class:
Wait: public final void wait () throws InterruptedException
Wake up the first waiting thread: public final void notify ()
Wake up all waiting threads: public final void notifyAll ()
Code example:
* producer-consumer entity * YangKuo 18:21 on 2018-11-15 * / public class Info {private String title; private String content; / * flag=true indicates that it can be produced, but cannot be taken away. * flag=false means it cannot be produced, and * / private Boolean flag=true can be taken away. Public synchronized void setInfo (String title, String content) throws InterruptedException {if (! flag) {/ / cannot be produced at this time, waiting for super.wait ();} Thread.sleep (100); this.title = title; this.content = content; flag = false;// production completed super.notify () } public synchronized void getInfo () throws InterruptedException {if (flag) {/ / should not take away super.wait (); / / wait} Thread.sleep (100); System.out.println (this.title + "- >" + this.content); flag = true;// consumption completed super.notify () }} / * producer * YangKuo 18:26 on 2018-11-15 * / public class Producer implements Runnable {private Info info; public Producer (Info info) {this.info = info;} @ Override public void run () {for (int x = 0; x < 50) ) {if (x% 2 = = 0) {try {info.setInfo ("Zhang San", "a handsome guy");} catch (InterruptedException e) {e.printStackTrace () }} else {try {info.setInfo ("little white rabbit", "a lovely little animal");} catch (InterruptedException e) {e.printStackTrace () } / * Consumer * YangKuo 2018-11-15 18:26 * / public class Customer implements Runnable {private Info info; public Customer (Info info) {this.info = info;} @ Override public void run () {for (int x = 0; x < 50) X catch +) {try {info.getInfo ();} catch (InterruptedException e) {e.printStackTrace ();} / * producer, consumer test * YangKuo 18:35 on 2018-11-15 * / public class Test {public static void main (String args []) {Info info = new Info () Producer p = new Producer (info); Customer c = new Customer (info); new Thread (p) .start (); new Thread (c) .start ();}}
Supplement: the difference between wait () and Sleep methods
Wait (): the method defined by the Object class, indicating that the thread must be woken up by the notify (), notifyAll () method to wait for execution.
Sleep (): is a method defined by the Thread class that wakes itself up after sleeping for a certain amount of time.
These are all the contents of the article "what are the application scenarios of Java threads?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.