public class MyThread extends Thread {volatile boolean keepRunning = true;int count = 0;//成员变量public void run() {System.out.println(getName() + "是一个Thread线程:");System.out.println(getName() + "线程开始了");while (keepRunning) {System.out.println(getName() + "启动:" + (++count));if (count == 100) {keepRunning = false;}if (count % 10 == 0) {try {Thread.sleep(1000);//若果count%10==0让线程休眠1s} catch (InterruptedException e) {e.printStackTrace();}}}System.out.println(getName() + "线程结束了");}public static void main(String[] args) {//main启动后立即结束,其他线程再执行,是一个异步过程System.out.println(Thread.currentThread().getName() + "启动");Thread thread = new MyThread();//多态,父类指向子类thread.setName("thread");thread.start();Thread thread1=new Thread(new MyRunnable(),"runnable");thread1.start();System.out.println(Thread.currentThread().getName() + "结束");}
}
class MyRunnable implements Runnable{volatile boolean keepRuning=true;int count=0;@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+"线程开始了");while (keepRuning) {System.out.println(Thread.currentThread().getName()+"运行了:"+(count++));if(count==100){keepRuning=false;}if(count%10==0){try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}System.out.println(Thread.currentThread().getName()+"线程结束了");}
}
详细解决方案
线程继承Thread和implements Runnable 交替运行
热度:0 发布时间:2023-12-22 16:00:02.0
相关解决方案
- thread(线程)中用spring注入不成功有关问题
- 这个是什么东东 Pool thread stack traces
- Thread.sleep() 与 Thread.currentThread().sleep()差异
- Which methods guarantee that a thread will leave the running state解决方案
- java thread wait的用法解决方案
- 已经终止了IIS ,为什么application_end()没有执行 thread.Abort(),线程还在执行?
- AIX下的weblogic814 thread count参数该如何设置
- MyEclipse 装配插件后Myeclipse Could not open the editor: Invalid thread access
- webllogic linux装完起步报错Exception in thread "main" java.lang.NoClassDefFoundError
- SVN异常:Thread attempted to read nesting count of a lock it did not own
- exception in thread "main" java.lang.NoSuchMethodError:main
- 常见异常 不能打开注册表关键字 'Temporary (volatile) Jet DSN for process 0xbec Thread 0xd34 DBC 0x3510024 Jet'
- PHP中VC6 X86跟VC9 X86的区别及Non Thread Safe的意思
- PHP中VC6 X86跟VC9 X86的区别及 Non Thread Safe的意思
- PHP版本VC6与VC9、Thread Safe与None-Thread Safe等的差异
- 5_3_x_nts_comp版本的ZendDebugger在PHP5.3(Thread Safety)下安装不成功的原因(转)
- TOMCAT6配置:thread stack size的值解决办法
- 兄弟们哪位高手遇见过这个一样?Can't create a new thread (errno 12)
- 大神,Thread 、synchronized出有关问题了
- Java Thread,关于wait和notify的有关问题
- ThreadPoolExecutor worker 错误导致core thread size减少
- jdeveloper implements,该如何处理
- Java语言导学札记 Chapter 8 Thread
- 关于线程池报unable to create new native thread,该怎么处理
- eclipse生成可执行jar-runnable jar
- exception in thread "main" java.lang.NoClassdefFoundError: com/caucho/server/htt,该怎么解决
- java线程有关问题,thread.sleep()
- 牛人进,为什么Thread(Runnable)小弟我如何也传不进
- Thread.sleep() 与 Thread.currentThread().sleep()差别
- Full thread dump,该怎么处理