In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to use Java to implement multithreaded switching waiting for waking up to print odd and even numbers alternately. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Introduction
In daily work and life, it may be useful for several or many people to do the same thing. In java programming, a similar situation also occurs. Multiple threads do the same job. For example, in the railway station ticket buying system, multiple people cannot buy the same ticket. When a window (thread) is selling a ticket, other windows (threads) are no longer allowed to sell this ticket. In this process, there is a problem of locking and resource waiting, how to reasonably and correctly ensure that a thread and a thread do not grab resources and wait for a situation that has been working all the time. Next, let's talk about the process of waking up and switching between multiple threads. Here, take the example of two threads An and B printing odd and even numbers alternately. The code is as follows:
Package com.svse.thread;import java.util.concurrent.atomic.AtomicInteger;/** * alternately print odd and even numbers * function description: * @ author:zsq * create date: 4:34:30 on May 27th, 2019 * modifier time to modify description * * Copyright (c) 2019 Beijing Zhihua Tiancheng Technology Co., Ltd.-copyright * / public class AlternatePrinting {/ / Let two threads use the same lock. Execute alternately. / / determine whether it is odd if it is odd and enter the odd thread to print and add one. The thread then releases the lock resource. Then let the thread wait / / to determine whether it is an even number, and if it is an even number enter the even number thread to perform printing and add one. The thread then releases the lock resource. Then let the thread wait for public static AtomicInteger atomicInteger = new AtomicInteger (1); public static void main (String [] args) {Thread a=new Thread (new AThread ()); Thread b=new Thread (new BThread ()); a.start (); b.start () } / / Odd thread public static class AThread implements Runnable {public void run () {while (true) {synchronized (atomicInteger) {if (atomicInteger.intValue ()% 2! = 0) {System.out.println ("Odd thread:" + atomicInteger.intValue ()) Try {Thread.sleep;} catch (InterruptedException E1) {/ / TODO Auto-generated catch block e1.printStackTrace ();} atomicInteger.getAndIncrement (); / / adds the current value 1 atomically. / / the odd thread releases the lock resource atomicInteger.notify (); / / releases the lock after the operation and enters waiting for try {atomicInteger.wait ();} catch (InterruptedException e) {e.printStackTrace () }} else {/ / Odd threads wait for try {atomicInteger.wait ();} catch (InterruptedException e) {/ / TODO Auto-generated catch block e.printStackTrace () Public static class BThread implements Runnable {public void run () {while (true) {synchronized (atomicInteger) {if (atomicInteger.intValue ()% 2 = = 0) { System.out.println ("even threads:" + atomicInteger.intValue ()) Try {Thread.sleep;} catch (InterruptedException E1) {/ / TODO Auto-generated catch block e1.printStackTrace ();} atomicInteger.getAndIncrement (); / / adds the current value 1 atomically. / / even thread releases lock resource atomicInteger.notify (); / / releases lock after operation and enters waiting for try {atomicInteger.wait ();} catch (InterruptedException e) {e.printStackTrace () }} else {try {/ / even threads wait for atomicInteger.wait ();} catch (InterruptedException e) {/ / TODO Auto-generated catch block e.printStackTrace () Thank you for reading! On "how to use Java to achieve multithreaded switching waiting for alternately printing odd and even numbers" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!
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.