Multithread y, policyall, and wait practices

Source: Internet
Author: User

 

 

  1. Package com. Rx;
  2. Class mythread_1 extends thread
  3. {
  4. Object lock;
  5. Public mythread_1 (Object O)
  6. {
  7. Lock = O;
  8. }
  9. Public void run ()
  10. {
  11. Try
  12. {
  13. Synchronized (LOCK)
  14. {
  15. System. Out. println ("Enter thread_1 and wait ");
  16. Lock. Wait ();
  17. // Lock. Wait (12222 );
  18. System. Out. println ("be notified ");
  19. }
  20. } Catch (interruptedexception e ){
  21. System. Out. println ("exce ");
  22. }
  23. }
  24. }
  25. Class mythread_2 extends thread
  26. {
  27. Object lock;
  28. Public mythread_2 (Object O)
  29. {
  30. Lock = O;
  31. }
  32. Public void run ()
  33. {
  34. Synchronized (LOCK)
  35. {
  36. System. Out. println ("Enter thread_2 and to define y ");
  37. Lock. Policy ();
  38. // Lock. policyall ();
  39. }
  40. }
  41. }
  42. Public class mythread
  43. {
  44. Public static void main (string [] ARGs) throws interruptedexception
  45. {
  46. // Int [] In = new int [0]; // notice
  47. Object in = new object ();
  48. Mythread_1 T1 = new mythread_1 (in );
  49. Mythread_2 t2 = new mythread_2 (in );
  50. // Mythread_1 t13 = new mythread_1 (in );
  51. T1.start ();
  52. // Thread. Sleep (12222 );
  53. // T13.start ();
  54. T2.start ();
  55. }
  56. }
The running result is as follows:
  1. Enter thread_1 and wait
  2. Enter thread_2 and to sort y
  3. Be notified

Run the policyall instance test:

  1. Package com. Rx;
  2. Class mythread_1 extends thread
  3. {
  4. Object lock;
  5. Public mythread_1 (Object O)
  6. {
  7. Lock = O;
  8. }
  9. Public void run ()
  10. {
  11. Try
  12. {
  13. Synchronized (LOCK)
  14. {
  15. System. Out. println ("Enter thread_1 and wait ");
  16. Lock. Wait ();
  17. // Lock. Wait (12222 );
  18. System. Out. println ("be notified ");
  19. }
  20. } Catch (interruptedexception e ){
  21. System. Out. println ("exce ");
  22. }
  23. }
  24. }
  25. Class mythread_2 extends thread
  26. {
  27. Object lock;
  28. Public mythread_2 (Object O)
  29. {
  30. Lock = O;
  31. }
  32. Public void run ()
  33. {
  34. Synchronized (LOCK)
  35. {
  36. System. Out. println ("Enter thread_2 and to define y ");
  37. // Lock. Y ();
  38. Lock. policyall ();
  39. }
  40. }
  41. }
  42. Public class mythread
  43. {
  44. Public static void main (string [] ARGs) throws interruptedexception
  45. {
  46. // Int [] In = new int [0]; // notice
  47. Object in = new object ();
  48. Mythread_1 T1 = new mythread_1 (in );
  49. Mythread_2 t2 = new mythread_2 (in );
  50. Mythread_1 t13 = new mythread_1 (in );
  51. T1.start ();
  52. // Thread. Sleep (12222 );
  53. T13.start ();
  54. T2.start ();
  55. }
  56. }

The running result is as follows:

  1. Enter thread_1 and wait
  2. Enter thread_1 and wait
  3. Enter thread_2 and to sort y
  4. Be notified
  5. Be notified

 

  1. Package com. Rx;
  2. Class mythread_1 extends thread
  3. {
  4. Object lock;
  5. Public mythread_1 (Object O)
  6. {
  7. Lock = O;
  8. }
  9. Public void run ()
  10. {
  11. Try
  12. {
  13. Synchronized (LOCK)
  14. {
  15. System. Out. println ("Enter thread_1 and wait ");
  16. // Lock. Wait ();
  17. Lock. Wait (10 );
  18. System. Out. println ("be notified ");
  19. }
  20. } Catch (interruptedexception e ){
  21. System. Out. println ("exce ");
  22. }
  23. }
  24. }
  25. Class mythread_2 extends thread
  26. {
  27. Object lock;
  28. Public mythread_2 (Object O)
  29. {
  30. Lock = O;
  31. }
  32. Public void run ()
  33. {
  34. Synchronized (LOCK)
  35. {
  36. System. Out. println ("Enter thread_2 and to define y ");
  37. // Lock. Y ();
  38. Lock. policyall ();
  39. }
  40. }
  41. }
  42. Public class mythread
  43. {
  44. Public static void main (string [] ARGs) throws interruptedexception
  45. {
  46. // Int [] In = new int [0]; // notice
  47. Object in = new object ();
  48. Mythread_1 T1 = new mythread_1 (in );
  49. Mythread_2 t2 = new mythread_2 (in );
  50. // Mythread_1 t13 = new mythread_1 (in );
  51. T1.start ();
  52. Thread. Sleep (1000 );
  53. // T13.start ();
  54. T2.start ();
  55. }
  56. }
The running result is as follows:
  1. Enter thread_1 and wait
  2. Be notified
  3. Enter thread_2 and to sort y

 

Test it on your own in other cases.

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.