什么题意啊?怎么读不明白,是线程每创建出一个的时候就打印“创建线程x”?大体做了一个,你看看符不符合题意,我没搞明白,具体到底干什么?
- class MyThread implements Runnable{
-
- private int num;
- public void run()
- {
- while(true)
- {
- //多线程操作了共享资源num,加同步
- synchronized(MyThread.class)
- {
- try {
- Thread.sleep(300);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if(num<10)
- System.out.println(Thread.currentThread().getName()+":计数"+num++);
- else
- return;
- }
- }
- }
-
- }
- public class ReflectDemo1 {
- public static void main(String[] args) {
- MyThread mt = new MyThread();
- for(int i = 0;i<5;i++)
- {
-
- new Thread(mt,"线程"+i).start();
- }
-
- }
- }
复制代码 |