Examples of threading operations

Source: Internet
Author: User

Instance requirements:

Design a threading action class that requires the ability to produce three thread objects, and can set the sleep time of three threads, respectively. As shown below:

Ask how to design?

Analysis

From the previous learning that there are two ways to implement a thread, one is to inherit the thread class, and the other is to implement the Runnable interface. and the thread name and sleep time properties should be saved in the class.

1, implemented through the thread class.

  the name attribute exists directly in the thread class . So instead of defining the Name property, you can set it directly in the subclass construction method through the super (name) method .

classMyThreadextendsthread{private int time; Time to define for yourself PublicMyThread (String name,intTime ) {        super (name); Set the thread name, because the thread itself has the name attribute, so you do not have to define the Name property, directly in the subclass construction method through the super (name) method setting         This. Time = time;//Set sleep Time    }     Public voidrun () {Try{ Thread.Sleep ( this.time); Hibernate the specified time}Catch(interruptedexception e) {e.printstacktrace ();        Print error. } System.out.println (Thread.CurrentThread (). GetName ()+ "Thread, hibernate" + This. Time + "milliseconds. ") ; }}; Public classexecdemo01{ Public Static voidMain (String args[]) {MyThread MT1=NewMyThread ("Thread A", 10000);//defining a Thread object, specifying sleep timeMyThread mt2 =NewMyThread ("Thread B", 20000);//defining a Thread object, specifying sleep timeMyThread MT3 =NewMyThread ("Thread C", 30000);//defining a Thread object, specifying sleep timeMt1.start ();//Start ThreadMt2.start ();//Start ThreadMt3.start ();//Start Thread    }};

Operation Result:

The number of seconds to sleep after the execution of threads.

Thread A thread, sleeps 10000 milliseconds. Thread B thread, sleeps 20000 milliseconds. Thread C thread, sleeps 30000 milliseconds.
2, implemented using the Runnable interface.

There is no Name attribute in the class, so a name property should be established separately to hold the thread's names.

  

classMyThreadImplementsrunnable{PrivateString name; Private intTime ;  PublicMyThread (String name,intTime ) {         This. name = name;//set the thread name         This. Time = time;//Set sleep Time    }     Public voidrun () {Try{ Thread.Sleep ( this.time); Hibernate the specified time}Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ( This. Name + "thread, hibernate" + This. Time + "milliseconds. ") ; }}; Public classexecdemo02{ Public Static voidMain (String args[]) {MyThread MT1=NewMyThread ("Thread A", 10000);//defining a Thread object, specifying sleep timeMyThread mt2 =NewMyThread ("Thread B", 20000);//defining a Thread object, specifying sleep timeMyThread MT3 =NewMyThread ("Thread C", 30000);//defining a Thread object, specifying sleep time        NewThread (MT1). Start ();//Start Thread        NewThread (MT2). Start ();//Start Thread        NewThread (MT3). Start ();//Start Thread    }};

Examples of threading operations

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.