文档介绍:,并重新定义run()方法实例化您自定义的Thread类别使用start()lassEraserThreadextendsThread{privatebooleanactive;privateStringmask;…//重新定义run()方法publicvoidrun(){while(isActive()){(mask); try{//暂停目前的线程50毫秒 ().sleep(50);}catch(InterruptedExceptione){();}}}}继承Thread//启动Eraser线程EraserThreaderaserThread=newEraserThread('#');();Stringpassword=();(false);()()方法,就可以避免输入的密码被窥视的问题实作Runnable界面如果您的类别已经要继承某个类别,那么您就不能继承Thread类别继承了Thread类别,()方法要实作在实例化一个Thread对象时,lassEraserimplementsRunnable{//实作Runnableprivatebooleanactive;privateStringmask;…//重新定义run()方法publicvoidrun(){while(isActive()){(mask); try{//暂停目前的线程50毫秒 ().sleep(50);}catch(InterruptedExceptione){();}}}}实作Runnable界面//Eraser实作Runnable界面Erasereraser=newEraser('#');//启动Eraser线程ThreaderaserThread=newThread(eraser);();Stringpassword=();(false);Daemon线程一个Daemon线程是一个在背景执行服务的线程如果所有的非Daemon的线程都结束了,则Daemon线程自动就会终止从Main方法开始的是一个非Daemon线程如果希望某个线程在产生它的线程结束后跟着终止,要将它设为Daemon线程Daemon线程Threadthread=newThread(//这是匿名类别的写法newRunnable(){publicvoidrun(){while(true){("T");}}});//(true);();使用setDaemon()方法来设定一个线程是否为Daemon线程预设所有从Daemon线程产生的线程也是Daemon线程线程生命周期执行start()之后,线程进入Runnable状态,此时线程尚未真正开始执行必须等待排班器(Scheduler)的排班