Java multithreaded synchronization--producer consumer issues

Source: Internet
Author: User

This is a model of a producer consumer problem in a Java video tutorial by a horse soldier teacher.

[Java]View Plaincopy
  1. Public class produceconsumer{
  2. public static void Main (string[] args) {
  3. Syncstack ss = new Syncstack ();
  4. Producer Pro = new Producer (ss);
  5. Consumer con = new Consumer (ss);
  6. new Thread (PRO). Start ();
  7. new Thread (Con). Start ();
  8. }
  9. }
  10. Class product{
  11. int id;
  12. Public Product (int id) {
  13. this.id = ID;
  14. }
  15. Public String toString () {
  16. return "Product:" + ID;
  17. }
  18. }
  19. Class syncstack{
  20. int index = 0;
  21. product[] Arrpro = new product[6];
  22. public synchronized void push (Product p) {
  23. While (index = = arrpro.length) {
  24. try {
  25. this.wait ();
  26. } catch (Interruptedexception e) {
  27. //TODO auto-generated catch block
  28. E.printstacktrace ();
  29. }
  30. }
  31. this.notify ();
  32. Arrpro[index] = p;
  33. index++;
  34. }
  35. public Synchronized Product pop () {
  36. While (index = = 0) {
  37. try {
  38. this.wait ();
  39. } catch (Interruptedexception e) {
  40. //TODO auto-generated catch block
  41. E.printstacktrace ();
  42. }
  43. }
  44. this.notify ();
  45. index--;
  46. return Arrpro[index];
  47. }
  48. }
  49. Class Producer implements runnable{
  50. Syncstack SS = null;
  51. Public Producer (Syncstack ss) { //holds a reference to Syncstack
  52. THIS.SS = SS;
  53. }
  54. @Override
  55. public Void Run () {
  56. For (int i=0; i<; i++) {
  57. Product P = New product (i);
  58. Ss.push (P);
  59. System.out.println ("produced:" + P);
  60. try {
  61. Thread.Sleep (100);
  62. } catch (Interruptedexception e) {
  63. E.printstacktrace ();
  64. }
  65. }
  66. }
  67. }
  68. Class Consumer implements runnable{
  69. Syncstack SS = null;
  70. Public Consumer (Syncstack ss) { //holds a reference to Syncstack
  71. THIS.SS = SS;
  72. }
  73. @Override
  74. public Void Run () {
  75. For (int i=0; i<; i++) {
  76. Product p = ss.pop ();
  77. System.out.println ("consumed:" + P);
  78. try {
  79. Thread.Sleep (1000);
  80. } catch (Interruptedexception e) {
  81. E.printstacktrace ();
  82. }
  83. }
  84. }
  85. }

Java multithreaded synchronization--producer consumer issues

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.