Java multithreading recommended Stop method and pause method

Source: Internet
Author: User

Determine thread end and let thread end

Package cn.lonecloud.thread.study;/** * Threads for Looping 1000 times * @Title: Run1000thread.java * @Package Cn.lonecloud.Thread.study * @Description:  * @author Lonecloud * @date August 14, 2016 PM 11:06:07 */public class Run1000thread extends thread{@Override public void Run () {for (int i = 0; i < i++) {System.out.println (i); if (this.interrupted ()) {//To determine if the thread has ended try {th Row new Interruptedexception ();//Used to stop thread} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}}

pausing threads and recovering threads

 PackageCn.lonecloud.Thread.study.resume;ImportOrg.apache.log4j.Logger; Public classMaintest { Public StaticLogger Logger=logger.getlogger (maintest.class);  Public Static voidMain (string[] args)throwsException {resumethread thread=NewResumeThread ();        Thread.Start (); Logger.debug (Start); Thread.Sleep (1000); Thread.Suspend ();//get this thread to stopLogger.debug ("I am Dormant"); Thread.Sleep (1000); Thread.Resume ();//Get thread ContinueLogger.debug ("I am booting"); }}

Thread class

 Package Cn.lonecloud.Thread.study.resume; Import Org.apache.log4j.Logger;  Public class extends thread{    publicstatic Logger Logger=logger.getlogger (resumethread.  Class);    @Override    publicvoid  run () {        logger.debug ("Start");        System.out.println ("Hello World");}    }

Stop and Pause methods recommended by Java multithreading

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.