Java Concurrent Programming 4

Source: Internet
Author: User

Description: First on the code, note follow-up supplement. 
public class ScheduleTest1 {

private static long start;

private static Scheduledexecutorservice Executorservice = Executors.newscheduledthreadpool (2);

/**
* If the output after Timer.schedule plus sleep is 1001 6005
* The sleep of the first thread affects the second thread
* If the first thread is dead, the Gothic thread will not execute
*timer is associated with system time.
*
* The above problems will not occur with Executorservice
*
* @param args
*/
public static void Main (string[] args) {
TimerTask task = new TimerTask () {
@Override
public void Run () {
System.out.println (System.currenttimemillis ()-start);
try {
Thread.Sleep (5000);
} catch (Interruptedexception e) {
E.printstacktrace ();
// }
throw new RuntimeException ();
}
};

TimerTask Task1 = new TimerTask () {
@Override
public void Run () {
System.out.println (System.currenttimemillis ()-start);
}
};

Timer timer = new timer ();
Start = System.currenttimemillis ();
1s after execution
Timer.schedule (task,1000);
3s after execution
Timer.schedule (task1,3000);

Executorservice.schedule (task,1000, timeunit.milliseconds);
Executorservice.schedule (task1,3000, timeunit.milliseconds);
}
}

Java Concurrent Programming 4

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.