(next first) Java multithreaded programming tutorial

Source: Internet
Author: User

The runnable interface should be implemented by classes that intend to execute their instances through a thread. The class must define a parameterless method named Run (). void Run ().

In addition, Runnable provides an activation method for non-thread subclasses. By instantiating a thread instance and running itself as a target, you can run a class that implements runnable without having to create a subclass of thread.


4, Timer TimerTask

TimerTask is a special thread that is used to schedule a timer to perform or perform a recurring task. He implements the Runable interface, which is an abstract class

Public abstract class TimerTask extends Object implements Runnable

She does not implement the run function itself, so a subclass is required to implement it.

Import Java.util.date;import Java.util.timer;import Java.util.timertask;public class Testtimertask extends TimerTask{        public void Run () {System.out.println (New Date ());} Every 1 seconds, the print time public static void main (string[] args) {Timer timer = new timer (); Testtimertask task = new Testtimertask (); Timer.schedule (task,1000,1000);}}

A timer task differs from a thread in that it is a timed thread, so it is not possible to start it directly using Start (), and a special class timer is used to start it. A timer is a tool that is used to schedule tasks that are performed in a background thread. You can schedule a task to execute once, or repeat it periodically.

After the last reference to the timer object is complete and all the unhandled tasks have been completed, the task execution thread of the timer terminates gracefully and becomes the object of garbage collection, but this can take a long time. So to quickly terminate the task execution thread of the timer, then call the Cancle method of the timer. void Cancle ();

There are several ways the timer runs:

    • Schedule the specified task to execute at the specified time void schedule (TimerTask task,date times)

    • Schedules the specified task to begin repeating a fixed delay at a specified time to execute void schedule (TimerTask task,date firsttime,long period);

    • Schedules the specified task to execute after a specified delay of void schedule (TimerTask task,long delay);


5. Use thread groups.

Threads can be organized, each thread in Java is a member of a thread group, and a thread group makes multiple thread sets an object through which thread groups can operate on multiple threads simultaneously, such as all threads that start a thread group. The thread Group for Java is implemented through the Threadgroup class.

The Threadgroup class is used to manage a set of threads, including the number of threads, the relationship between threads, what the thread is doing, and how long the thread will start or end. Thread groups can also contain thread groups.

The advantage of using a thread group is that you can work on a group of threads as a whole, and the available action functions include

    • Resume ();//Resume all processing of the thread group

    • Stop ();//stops all processing in this thread group

    • Suspend ();//suspend all processing of this thread group

    • Destroy ();//revoke the thread group and all its child threads



This article is from the "Ah Cool blog source" blog, please make sure to keep this source http://aku28907.blog.51cto.com/5668513/1773021

(next first) Java multithreaded programming tutorial

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.