加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

一个对象可以在Java中创建多个线程

发布时间:2020-12-15 04:19:33 所属栏目:Java 来源:网络整理
导读:我是多线程新手,在练习时我编写了以下代码.我想调用createThred方法创建一个我称之为的新线程evrytime.但是,使用下面给出的代码,每次调用createThread方法时,我都会反复运行相同的线程.是否可以使用同一对象创建新线程?显然不是,只是想确认是否有一种我不知
我是多线程新手,在练习时我编写了以下代码.我想调用createThred方法创建一个我称之为的新线程evrytime.但是,使用下面给出的代码,每次调用createThread方法时,我都会反复运行相同的线程.是否可以使用同一对象创建新线程?显然不是,只是想确认是否有一种我不知道的方式.

public class ThreadStartRunnable implements Runnable {

    private Thread t;
    /*
     ThreadStartRunnable(String name) {
         t = new Thread(this,name);
        t.start();
        }
     */
       private Thread createThread(String name){

         t = new Thread(this,name);
         t.start();
         return t;
     }

    /**
     * @Override run
     * 
     */
    public void run() {

        for(int i=0;i<=5;i++)
        {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("The current Thread is " + t.getName() + " and thread ID is " + t.getId());
        }
    }
    /**
     * @param args
     * @throws InterruptedException 
     */
    public static void main(String[] args)  {
        // TODO Auto-generated method stub

        Thread t = Thread.currentThread();
        //new ThreadStartRunnable("User 1");
        //new ThreadStartRunnable("User 2");
        //ThreadStartRunnable c = new ThreadStartRunnable();
        ThreadStartRunnable t1 = new ThreadStartRunnable();

        t1.createThread("Child 1");
        t1.createThread("Child 2");

        for(int i=0;i<=5;i++)
        {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("The cirrent Thread is " + t.getName()+ " and thread ID is " + t.getId());
        }




    }

}

OUTPUT:

The current Thread is Child 2 and thread ID is 9

The current Thread is Child 2 and thread ID is 9

The cirrent Thread is main and thread ID is 1

The current Thread is Child 2 and thread ID is 9

The current Thread is Child 2 and thread ID is 9

The current Thread is Child 2 and thread ID is 9

The current Thread is Child 2 and thread ID is 9

The current Thread is Child 2 and thread ID is 9

The current Thread is Child 2 and thread ID is 9

The cirrent Thread is main and thread ID is 1

The current Thread is Child 2 and thread ID is 9

The current Thread is Child 2 and thread ID is 9

The current Thread is Child 2 and thread ID is 9

The current Thread is Child 2 and thread ID is 9

The cirrent Thread is main and thread ID is 1

The cirrent Thread is main and thread ID is 1

The cirrent Thread is main and thread ID is 1

The cirrent Thread is main and thread ID is 1

解决方法

在run方法中更改代码以显示当前线程的名称:

System.out.println("The current Thread is " + Thread.currentThread().getName() + " and thread ID is " + Thread.currentThread().getId());

每次创建新线程时都会覆盖实例变量t,因此使用它来跟踪线程名称和ID是不安全的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读