Java Thread producer Consumer

Source: Internet
Author: User

See the Bi Xiangdong Teacher's producer consumers, just follow the video reference run a bit, feel okay

What is worth learning is that the organization is particularly clear:

Producterconsumerdemo.java, a resource class resources, the producer consumers can access to.

Producer Class Producter, consumer consumer have implemented the Runnable interface, in which the Run method to implement overloading, the sharing of resources for production and consumption

Optimization:

If you later need to join the project, add a constructor method to the Producterconsumerdemo class, public Producterconsumerdemo () {...}, instantiate the object to invoke

Code:

1 classProducterconsumerdemo2 {3      Public Static voidMain (string[] args)4     {5Resources r =NewResources ();6Productor Pro =NewProductor (r);7Consumer con =NewConsumer (r);8         9Thread T1 =NewThread (pro);TenThread t2 =NewThread (con); One T1.start (); A T2.start (); -System.out.println ("Hello world!"); -     } the } -  - classResources - { +     PrivateString name; -     Private intCount =1; +     Private BooleanFlag =false; A  at      Public synchronized voidset (String name) -     { -        if(flag) -            Try{ This. Wait ();}Catch(Exception e) {} -         This. Name = name+ "--" +count++; -  inSystem.out.println (Thread.CurrentThread (). GetName () + "producer" + This. Name); -Flag =true; to        //Wake up the other party process +         This. Notify (); -  the     } *      Public synchronized voidout () $     {Panax Notoginseng        if(!flag) -            Try{ This. Wait ();}Catch(Exception e) {} the         +System.out.println (Thread.CurrentThread (). GetName () + ".... Consumer .... "+ This. Name); AFlag =false; the        //Wake up the other party process +         This. Notify (); -  $     } $ } -  - classProductorImplementsRunnable the { -     PrivateResources Res;Wuyi Productor (Resources res) { the          This. res =Res; -     } Wu      Public voidrun () { -          while(true){ AboutRes.set ("+ + products + +"); $         } -     } -  - } A  + classConsumerImplementsRunnable the { -     PrivateResources Res; $ Consumer (Resources res) { the          This. res =Res; the     } the      Public voidrun () { the          while(true){ - res.out (); in         } the     } the  About}

Because the function that you need is that the producer consumer operates on a two-dimensional array, the member properties of the resource resources class are added on top of it. Implement Access functions

It is a frame, retained:

1 classproducterconsumerarr2d2 {3      Public Static voidMain (string[] args)4     {5Resources r =NewResources ();6Productor Pro =NewProductor (r);7Consumer con =NewConsumer (r);8         9Thread T1 =NewThread (pro);TenThread t2 =NewThread (con); One T1.start (); A T2.start (); -System.out.println ("Hello world!"); -     } the } -  - classResources - { +     PrivateString name; -     Private intCount =1; +     Private float[] arr2d=New float[5] [5];//allocate 5 rows and 5 columns to the space size A     Private BooleanFlag =false; at  -      Public synchronized voidset (String name) -     { -        if(flag) -            Try{ This. Wait ();}Catch(Exception e) {} -        //need to be placed in front of count++ in          for(inti=0;i<5;i++) -             for(intj=0;j<5;j++) to         { +Arr2d[i][j]= (float) count; -         } the         This. Name = name+ "--" +count++; *System.out.println (Thread.CurrentThread (). GetName () + "producer" + This. Name); $       Panax NotoginsengFlag =true; -        //Wake up the other party process the         This. Notify (); +  A     } the      Public synchronized voidout () +     { -        if(!flag) $            Try{ This. Wait ();}Catch(Exception e) {} $         -System.out.println (Thread.CurrentThread (). GetName () + ".... Consumer .... "+ This. name+ "Any element of a two-dimensional array" +arr2d[2][2]); -Flag =false; the        //Wake up the other party process -         This. Notify ();Wuyi  the     } - } Wu  - classProductorImplementsRunnable About { $     PrivateResources Res; - Productor (Resources res) { -          This. res =Res; -     } A      Public voidrun () { +          while(true){ theRes.set ("+ + two-dimensional array access + +")); -         } $     } the  the } the  the classConsumerImplementsRunnable - { in     PrivateResources Res; the Consumer (Resources res) { the          This. res =Res; About     } the      Public voidrun () { the          while(true){ the res.out (); +         } -     } the Bayi}
Producterconsumerarr2d.java

Of course, this is only for supporting two separate threads, if you continue to join multiple threads (>2) will certainly have the problem of resource data error, continue to learn

Java Thread producer Consumer

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.