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
相关解决方案
- Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml解决办法
- thread(线程)中用spring注入不成功有关问题
- Exception in thread "main" java.lang.UnsupportedClassVersionError:有关问题
- Thread.sleep() 有关问题
- Exception in thread "main" java.lang.NullPointerException,该怎么处理
- 小弟我的程序中Exception in thread "main" java.lang.NullPointerException如何解决
- Exception in thread "main" java.lang.UnsatisfiedLinkError: com.
- Exception in thread "main" java.lang.InstantiationError:
- Exception in thread "Thread-7" java.lang.UnsatisfiedLinkError:
- Exception in thread "main" java.lang.NoClassDefFound:HelloWorld,该怎么处理
- 这个是什么东东 Pool thread stack traces
- in thread "main" java.lang.OutOfMemoryError: Java heap space
- Exception in thread "main"不知名的异常!
- Exception in thread "main" java.security.InvalidKeyException:
- 上面的代码会出错,Exception in thread "main" java.lang.Error:
- Thread.sleep() 与 Thread.currentThread().sleep()差异
- 报错 Exception in thread "main" java.lang.NoClassDefFound:HelloWorld,该怎么解决
- Which methods guarantee that a thread will leave the running state解决方案
- 出现Exception in thread "main" java.lang.NullPointerException求赐教,
- Exception in thread "main" java.lang.NullPointerException求大牛帮忙解决,30行小程序解决方案
- Exception in thread "main" java.lang.NullPointerException如何解决
- 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
- Exception in thread "main" java.lang.NullPointerException是什么意 ...
- [求助]学习线程时的一个困惑,new Thread(Runnable r)