问题描述
importjava.io.IOException;importjava.util.Timer;importjava.util.TimerTask;publicclassTimerTest{publicstaticvoidmain(String[]args){//启动一个模拟的数量加一MessageUploadthread=newMessageUpload();thread.start();//timer的作用是每隔一秒种打印一次//打印的数据来源于MessageUpload中的数据Timertimer=newTimer();timer.schedule(newmyTask(thread),1000,1000);//无限循环,直到出现c的时候自动退出while(true){try{intch=System.in.read();if(ch-'c'==0){timer.cancel();break;}}catch(IOExceptione){e.printStackTrace();}}}}classmyTaskextendsTimerTask{MessageUploadthread;publicmyTask(MessageUploadthread){this.thread=thread;}@Overridepublicvoidrun(){System.out.println(thread.getCount());thread.setCount(0);}}classMessageUploadextendsThread{privateintcount;@Overridepublicsynchronizedvoidrun(){while(true){count++;}}publicintgetCount(){returncount;}publicvoidsetCount(intcount){this.count=count;}}
解决方案
解决方案二:
兄弟你的问题要要做什么?