Multi-threaded instance-display the system time every second (Java)

Source: Internet
Author: User

I know that I have learned programming through multiple threads, but now I really understand what he is used. It is like dividing a project into different directions, running the implementation at the same time, and finally finishing in the main direction. This achieves high efficiency and low consumption. The system time is displayed every second. This project is easy but can easily lead to an endless loop and the program cannot be jumped out, so we can easily implement it using multiple threads: [html] public class ClockThreadTest {/*** @ param args */public static void main (String [] args) {ClockThread clockThread = new ClockThread (); clockThread. start (); System. out. println ("end") ;}} class ClockThread extends Thread {@ Override public void run () {super. run (); while (true) {System. out. println (new Date (); try {Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace () ;}www.2cto.com }}do you see the specific embodiment of the thread? Every second shows that the system time is multi-thread, while the end is in the main thread. In this way, there will be no endless loops.

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.