Java Multithreading summary Three: Sleep (), join (), interrupt () example

Source: Internet
Author: User

This is an example from the Java programming mindset

[Java]View Plaincopy
  1. Package demo.thread;
  2. /**
  3. *sleep () is a static method that belongs to a class that is used to block the current thread
  4. *join () is a thread that synchronizes threads, such as calling T.join () in a thread to indicate that the T thread executes and then executes the current thread
  5. *interrupt () sets a flag to a thread indicating that the thread has been interrupted, but this flag will be cleaned up when the exception is captured
  6. * So in the catch clause, the flag is false
  7. */
  8. Public class Sleepjoindemo {
  9. public static void Main (string[] args) {
  10. Sleeper sleep1 = new Sleeper ("Sleep1", 1500);
  11. Sleeper SLEEP2 = new Sleeper ("SLEEP2", 1500);
  12. Joiner join1 = new Joiner ("Join1", SLEEP1);
  13. Joiner join2 = new Joiner ("join2", SLEEP1);
  14. Sleep2.interrupt ();
  15. }
  16. }
  17. Class Sleeper extends Thread {
  18. //can set the sleep time by the parameter
  19. private int sleeptime;
  20. Public Sleeper (String name, int sleeptime) {
  21. super (name);
  22. this.sleeptime = sleeptime;
  23. Start (); //Start in construction method
  24. }
  25. @Override
  26. public Void Run () {
  27. try {
  28. Sleep (Sleeptime);
  29. } catch (Interruptedexception e) {
  30. System.out.println (GetName () + "was interrupted.\n"
  31. + "isinterrupted ():" + isinterrupted ());
  32. return;
  33. }
  34. System.out.println (GetName () + "has awakened");
  35. }
  36. }
  37. Class Joiner extends Thread {
  38. private Sleeper Sleeper;
  39. Public Joiner (String name, Sleeper Sleeper) {
  40. super (name);
  41. this.sleeper = sleeper;
  42. Start ();
  43. }
  44. public Void Run () {
  45. try {
  46. Sleeper.join (); //merge, asynchronous Sync
  47. } catch (Interruptedexception e) {
  48. System.out.println ("interrupted");
  49. }
  50. System.out.println (GetName () + "join completed");
  51. }
  52. }

Java Multithreading summary Three: Sleep (), join (), interrupt () example

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.