Java concurrent exchanger example

Source: Internet
Author: User

Class databuffer {<br/> private int count; <br/> Public static int max_size = 100; </P> <p> Public databuffer (INT count) {<br/> This. count = count; <br/>}</P> <p> Public synchronized Boolean isfull () {<br/> return COUNT = max_size; <br/>}</P> <p> Public synchronized Boolean isempty () {<br/> return COUNT = 0; <br/>}</P> <p> Public void add (INT offset) {<br/> count + = offset; <br/>}</P> <p> Public void sub (int Offset) {<br/> count-= offset; <br/>}</P> <p> Public int size () {<br/> return count; <br/>}</P> <p> public class fillandempty {<br/> private exchanger <databuffer> exchanger = new exchanger <databuffer> (); <br/> private databuffer initialemptybuffer = new databuffer (0); <br/> private databuffer initialfullbuffer = new databuffer (databuffer. max_size); <br/> Private Static final int step = 2; </P> <p> Private void addtobuffer (databuffer) {<br/> databuffer. add (STEP); <br/>}</P> <p> private void takefrombuffer (databuffer) {<br/> databuffer. sub (STEP); <br/>}</P> <p> class fillingloop implements runnable {<br/> Public void run () {<br/> databuffer currentbuffer = initialemptybuffer; <br/> try {<br/> while (currentbuffer! = NULL) {<br/> addtobuffer (currentbuffer); <br/> If (currentbuffer. isfull () {<br/> currentbuffer = exchanger. exchange (currentbuffer); <br/> system. out. println (currentbuffer + "/t" <br/> + "currentbuffer. isfull () "); <br/>}< br/>} catch (interruptedexception ex) {<br/> thread. currentthread (). interrupt (); <br/>}</P> <p> class emptyingloop implements runnable {<br/> Public void run () {<Br/> databuffer currentbuffer = initialfullbuffer; <br/> try {<br/> while (currentbuffer! = NULL) {<br/> takefrombuffer (currentbuffer); <br/> If (currentbuffer. isempty () {<br/> thread. sleep (5000); <br/> currentbuffer = exchanger. exchange (currentbuffer); <br/> system. out. println (currentbuffer + "/t" <br/> + "currentbuffer. isempty () "); <br/>}< br/>} catch (interruptedexception ex) {<br/> thread. currentthread (). interrupt (); <br/>}</P> <p> void start () {<br/> New thread (New fillingloop ()). start (); <br/> New thread (New emptyingloop ()). start (); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> New fillandempty (). start (); <br/>}< br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.