Test the inter-Thread Synchronization in j2m2.

Source: Internet
Author: User

Y and wait are used for thread synchronization. Below are three simple examples;

Example 1:

Public class synobject {</P> <p> private thread m_synt; <br/> Object OBJ = new object (); </P> <p> Public void start () {<br/> m_synt = new synt (1); <br/> m_synt.start (); <br/> New synt (2 ). start (); <br/>}</P> <p> Public void process () {<br/> synchronized (OBJ) {<br/> obj. Y (); <br/>}</P> <p> private class synt extends thread {<br/> int id = 0; <br/> Public synt (INT value) {<br/> id = value; <br/>}< br/> Public void run () {<br/> int icount = 0; <br/> while (true) {<br/> system. out. println ("ID:" + ID + "Run:" + icount ++); <br/> try {<br/> sleep (100 ); <br/>} catch (interruptedexception IE) {</P> <p >}< br/> synchronized (OBJ) {<br/> try {<br/> obj. wait (); <br/>}catch (interruptedexception IE) {<br/> system. out. println ("ie:" + IE. getmessage (); <br/>}< br/>} 

In the MIDlet, press the key to trigger the process. Each time the process is pressed, it runs cyclically once. The process is controlled by the object obj!

 

Example 2:

Simple producer-consumer problem: one producer and one consumer

Public class queue {<br/> int value; <br/> Boolean bfull = false; </P> <p> Public synchronized void put (int I) {<br/> // synchronized (this) {<br/> If (! Bfull) {<br/> value = I; <br/> bfull = true; <br/> running y (); <br/>}< br/> try {<br/> wait (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> //} <br/>}< br/> Public synchronized int get () {<br/> // synchronized (this) {<br/> If (! Bfull) {</P> <p> try {<br/> wait (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> bfull = false; <br/> Y (); <br/>/}< br/> return value; <br/>}< br/>} 

Producer:

Class producer extends thread {<br/> queue Q; <br/> static int s_id = 0; <br/> int m_id = 0; <br/> producer (queue q) {<br/> This. Q = Q; <br/> m_id = s_id ++; <br/>}< br/> Public void run () {<br/> // synchronized (q) {<br/> for (INT I = 0; I <100; I ++) {<br/> q. put (I); <br/> system. out. println ("ID:" + m_id + "producer put" + I); <br/>}< br/>/}< br/>}< br/>} 

Consumer:

Class consumer extends thread {<br/> queue Q; <br/> static int s_id = 0; <br/> int m_id = 0; <br/> consumer (queue q) {<br/> This. Q = Q; <br/> m_id = s_id ++; <br/>}< br/> Public void run () {<br/> int value = 0; <br/> while (true) {<br/> // synchronized (q) {<br/> value = Q. get (); <br/> If (m_id = 1) {<br/> system. out. println (value); <br/>}< br/> // system. out. println ("ID:" + m_id + "consumer get" + q. get (); <br/>/}< br/>}< br/>} 

 

Example 3: Multiple producers and consumers

Public class queue2 extends queue {</P> <p> Private Static final int buff_len = 2; </P> <p> int [] m_iqueue = new int [buff_len]; <br/> int m_ireadindex = 0; <br/> int m_iwriteindex = 0; </P> <p> Private Static final int state_empty = 0; <br/> Private Static final int state_normal = 1; <br/> Private Static final int state_full = 2; </P> <p> int m_istate = state_normal; </P> <p> Object m_objr = new object (); <br/> Object m_objw = new object (); </P> <p> Public void put (INT value) {<br/> synchronized (m_objw) {<br/> If (m_iwriteindex + 1) % buff_len) = m_ireadindex) {<br/> // full <br/> system. out. println ("full"); <br/> m_istate = state_full; <br/> try {<br/> m_obj?wait (); <br/>} catch (interruptedexception IE) {</P> <p >}< br/>}</P> <p> synchronized (m_objr) {<br/> m_iwriteindex = (m_iwriteindex + 1) % buff_len; <br/> m_iqueue [m_iwriteindex] = value; </P> <p> If (m_istate = state_empty) {<br/> // system. out. println ("put policy"); <br/> m_istate = state_normal; <br/> m_objr.y y (); <br/>}</P> <p> Public int get () {<br/> synchronized (m_objr) {<br/> If (m_ireadindex = m_iwriteindex) {<br/> // empty <br/> m_istate = state_empty; <br/> system. out. println ("empty"); <br/> try {<br/> m_objr.wait (); <br/>} catch (interruptedexception IE) {</P> <p >}< br/>}</P> <p> int value = 0; </P> <p> synchronized (m_objw) {<br/> m_ireadindex = (m_ireadindex + 1) % buff_len; <br/> value = m_iqueue [m_ireadindex]; </P> <p> If (m_istate = state_full) {<br/> // system. out. println ("Get policy"); <br/> m_istate = state_normal; <br/> m_obj1_1_y (); <br/>}< br/> return value; <br/>}< br/>} 

In this case, two objects need to be used to control synchronization. One is used to control the producer and the other is used to control the consumer;

 

 

Related Article

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.