multithreading - Java re-doing a piece of code ... using threads -
let's have code:
public class helloworld { public static void main(string args[]) { system.out.println("hello world!"); } }
using threads, there way can make hello world echo continuously every 5 seconds?
this version repeats hello world message continuously, while allowing user terminate message-writing thread:
public class helloworld { public static void main(string[] args) throws exception { thread thread = new thread(new runnable() { public void run() { try { while (!thread.currentthread().isinterrupted()) { thread.sleep(5000); system.out.println("hello world!"); } } catch (interruptedexception e) { thread.currentthread().interrupt(); } } }); thread.start(); system.out.println("press key quit"); system.in.read(); thread.interrupt(); } }
Comments
Post a Comment