Hangs and restores of Java multi thread threads (suspend method and resume method)

Source: Internet
Author: User
Tags first string

First, introduce

This article discusses the characteristics of Java Multi-threading, using the Thread.Suspend () method to pause a thread, and use Thread.Resume () to resume a suspended thread.

Let's start with two basic knowledge about threading:

The executing body of the ① thread is the code inside the Run () method.

The ②thread.sleep () method causes the currently executing thread to sleep.

Two, suspend () method

① when a thread's suspend () method is called, the thread is suspended. If the thread occupies a lock, it does not release the lock. That is, the thread holds the lock in the suspended state.

②suspend () is already an outdated method.

To analyze a piece of code:

 Public classMyThreadextendsThread {Private Longi = 0;  Public LongGeti () {returni; }     Public voidSetI (Longi) { This. i =i; } @Override Public voidrun () { while(true) {i++; System.out.println (i);//Synchronization Method        }    }}            

1  Public classRun {2 3      Public Static voidMain (string[] args) {4 5         Try {6MyThread thread =NewMyThread ();7Thread.Start ();//start a thread of ' thread '8Thread.Sleep (1000);//causes the current thread (main thread) to sleep9Thread.Suspend ();//suspend thread ' thread 'TenSystem.out.println ("Main end!"); One}Catch(interruptedexception e) { A e.printstacktrace (); -         } -     } the  -}

On line 8th, the thread of sleep is the main thread. This way, the thread that starts on line 7th has the chance to get CPU execution, so the code in the Mythread class's run () method executes.

When the main thread sleeps for 1 seconds and gets the CPU executed again, it executes to line 9th.

On line 9th, let the thread starting in line 7th suspend (hang).

Therefore, the ' thread ' thread will no longer print the value of I. The main thread then proceeds to line 10th, ready to print "main end!"

However, because System.out.println (...) is a synchronous method, the source code for the printout println (Object O) is as follows:

1  /**2 * Prints an Object and then terminate the line. This method calls3 * At first string.valueof (x) to get the printed object ' s String value,4 * then behaves as5 * Though it invokes <code>{@link#print (String)}</code> and then6 * <code>{@link#println ()}</code>.7      *8      * @paramx The <code>Object</code> to is printed.9      */Ten      Public voidprintln (Object x) { OneString s =string.valueof (x); A         synchronized( This) { - print (s); - newLine (); the         } -}

As you can see, in line 12th, you need to get the lock of the current printout object first.

Because of this time, the thread ' threads ' of the Mythread class is suspended. It also has a print statement inside the run () method. Therefore, it occupies a printout object lock that is not released.

This causes the main thread to fail to execute line 10th in Run.java, which prints the output statement.

Note that printout is a static property in the system class, and there is only one PrintOut object in the system class, and the relevant source code in the System class is as follows:

/**      * The "standard" output stream. This stream was already     * open and ready to accept output data. Typically this stream     * corresponds to display output or another output destination     * specified by the host Enviro Nment or user.     * <p>     * For simple stand-alone Java applications, a typical-to-write     * A line of output data is:     * & Lt;blockquote><pre>     *     System.out.println (data)     * </pre></blockquote>     * <p>     * See the <code>println</code> methods in class <code>printstream</code>.      */public    finalstaticnull;

Three, resume () method

This method is very simple to restore a thread that is suspended due to the suspend () method, allowing it to regain CPU execution.

Hangs and restores of Java multi thread threads (suspend method and resume method)

Related Article

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.