- /*
- 需求:创建两个线程,和主线程交替运行
- */
- class Demo extends Thread
- {
- Demo(String s)
- {
- super(s);
- }
- public void run()
- {
- for (int x=0;x<60 ;x++ )
- {
- System.out.println(Thread.currentThread().getName()+"is running"+x);
- }
- }
- }
- class ThreadDemo
- {
- public static void main(String[] args)
- {
- Demo d=new Demo("线程1");
- Demo d1=new Demo("线程2");
- d.start();
- d1.start();
- for (int y=0;y<60 ;y++ )
- {
- System.out.println("Thread main is running!");
- }
- }
- }
复制代码 我写的多线程有问题吗?为啥有时结果不会交替运行呢?
|