题目1
package com; import java.awt.Graphics; import java.text.SimpleDateFormat; import java.util.Date; public class thread implements Runnable{ Thread clockThread; Date date; public void start() { if(clockThread==null){ clockThread=new Thread(); clockThread.start(); } } public void run() { while(clockThread!=null){ Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = sdf.format(date); System.out.println(time); } try{ clockThread.sleep(1000); }catch(InterruptedException e){ System.out.println(e); } } }
package com; public class currenttime { public static void main(String[] args) { // TODO Auto-generated method stub thread t=new thread(); t.start(); t.run(); } }
题目二
package com; import java.awt.Graphics; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class thread implements Runnable{ Thread clockThread; public void start() { if(clockThread==null){ clockThread=new Thread(); clockThread.start(); } } public void run() { Scanner s=new Scanner(System.in); System.out.println("input"); int c=s.nextInt(); int ran=((int)Math.random()*100); if(c>ran){ System.out.println("yes"); }else{ System.out.println("no"); } try{ clockThread.sleep(4000000); }catch(InterruptedException e){ System.out.println(e); } } }