Two ways for a sub-thread to interrupt the work of the main thread

Source: Internet
Author: User

1. thread. Join

The Code is as follows:

  1. Package demo;
  2. Import java. util. Random;
  3. Public class waitallsubthread {
  4. // Int livethreadnum; // record the number of running sub-threads
  5. Int N; // Number of worker threads
  6. Public waitallsubthread (int n ){
  7. This. n = N;
  8. }
  9. Class worker implements runnable {
  10. String name;
  11. Int sleep;
  12. Public worker (string name, int sleep ){
  13. This. Name = Name;
  14. This. Sleep = sleep;
  15. }
  16. Public void run (){
  17. // Uplive (); // calculate that this thread has been working.
  18. System. Out. println (name + ", start to work .");
  19. Try {
  20. Thread. Sleep (sleep); // virtual operation. 10 s random time
  21. } Catch (interruptedexception e ){
  22. System. Out. println (name + "interrupted .");
  23. }
  24. System. Out. println (name + ", end to work [" + sleep + "] Sleep .");
  25. // Downlive (); // This thread is finished
  26. }
  27. }
  28. //// Synchronous method for recording the number of threads.
  29. // Private synchronized void downlive (){
  30. // Livethreadnum --;
  31. //}
  32. //
  33. // Private synchronized void uplive (){
  34. // Livethreadnum ++;
  35. //}
  36. //
  37. // Private synchronized Boolean islive (){
  38. // Return livethreadnum> 0;
  39. //}
  40. Public void run (){
  41. System. Out. println ("------------- main run start -------------");
  42. Int sleepsaid = 10*1000; // virtual maximum time of each worker thread
  43. Random Rm = new random ();
  44. Thread [] ths = new thread [N];
  45. For (INT I = 0; I <ths. length; I ++ ){
  46. Ths [I] = new thread (new worker ("worker-" + I, Rm. nextint (sleepsaid) + 1 ));
  47. Ths [I]. Start ();
  48. }
  49. For (thread TH: ths ){
  50. Try {
  51. Th. Join (); // join method
  52. } Catch (interruptedexception e ){
  53. E. printstacktrace ();
  54. }
  55. }
  56. //// Wait for all worker threads to finish.
  57. // While (islive ()){
  58. // Try {
  59. // Thread. Sleep (1000); // check whether all threads are completed every 1 s.
  60. //} Catch (interruptedexception e ){
  61. // System. Out. println ("main thread sleep interrupted .");
  62. //}
  63. //}
  64. System. Out. println ("--------------- main run end --------------");
  65. }
  66. Public static void main (string [] ARGs ){
  67. Waitallsubthread wast = new waitallsubthread (10 );
  68. Wast. Run ();
  69. }
  70. }

2. countdownlatch

The Code is as follows:

  1. Package demo;
  2. Import java. util. Random;
  3. Import java. util. Concurrent. countdownlatch;
  4. Public class countdownlatchuse {
  5. Final countdownlatch downlatch;
  6. Int N; // Number of worker threads
  7. Public countdownlatchuse (int n ){
  8. This. downlatch = new countdownlatch (N );
  9. This. n = N;
  10. }
  11. Class worker implements runnable {
  12. String name;
  13. Int sleep;
  14. Public worker (string name, int sleep ){
  15. This. Name = Name;
  16. This. Sleep = sleep;
  17. }
  18. Public void run (){
  19. System. Out. println (name + ", start to work .");
  20. Try {
  21. Thread. Sleep (sleep); // virtual operation. 10 s random time
  22. } Catch (interruptedexception e ){
  23. System. Out. println (name + "interrupted .");
  24. }
  25. System. Out. println (name + ", end to work [" + sleep + "] Sleep .");
  26. Medone (); // completed by a worker thread
  27. }
  28. }
  29. Private void medone (){
  30. Downlatch. Countdown ();
  31. }
  32. Public void run (){
  33. System. Out. println ("------------- main run start -------------");
  34. Int sleepsaid = 10*1000; // virtual maximum time of each worker thread
  35. Random Rm = new random ();
  36. For (INT I = 0; I <n; I ++ ){
  37. New thread (new worker ("worker-" + I, Rm. nextint (sleepsaid) + 1). Start ();
  38. }
  39. Try {
  40. Downlatch. Await (); // wait for all worker threads to finish.
  41. } Catch (interruptedexception e ){
  42. System. Out. println ("Main interrupted .");
  43. }
  44. System. Out. println ("--------------- main run end --------------");
  45. }
  46. Public static void main (string [] ARGs ){
  47. Countdownlatchuse MTU = new countdownlatchuse (10 );
  48. MTU. Run ();
  49. }
  50. }

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.