Java 多线程在一次运行方法中执行不同的任务

Java 多线程在一次运行方法中执行不同的任务,java,multithreading,java-threads,Java,Multithreading,Java Threads,我是执行多线程的新手 我想要两个不同名称的线程。但这不会发生。取最后一个作为名称 下面是我的类的代码,它扩展了线程: public class Asallik extends Thread { public static ArrayList<Thread> t2; public static ArrayList<String> str; public static ArrayList<Asallik> d

我是执行多线程的新手

我想要两个不同名称的线程。但这不会发生。取最后一个作为名称

下面是我的类的代码,它扩展了线程:

    public class Asallik extends Thread {
        public static ArrayList<Thread> t2;
        public static ArrayList<String> str;
        public static ArrayList<Asallik> d;
        public int i = 0;
        public Asallik(String threadName) {
            t2.add(new Thread(this, threadName));
            t2.get(i).start();    
            i++;    
        }

        public static void createThread() {
            str = new ArrayList();
            t2 = new ArrayList();

            for (int i = 0; i < 2; i++) {
                str.add("Thread " + String.valueOf(i));
                d.add(new Asallik(str.get(i)));
            }

        }

        @Override
        public void run() {
            System.out.println("I came " + this.getName());
            if (t2.get(0).getName().equals("Thread 0")) {
                System.out.println("Thread 0 arrived");
            } else if (t2.get(1).getName().equals("Thread 1")) {
                System.out.println("Thread 1 arrived");
            }
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                System.out.println("Error : " + e);
            }
        }

    }
我试过这种方法,但没能成功。是否有一种方法可以在一个运行方法中执行多线程,但名称不同

结果如下:

I came Thread-0
Exception in thread "main" java.lang.NullPointerException
Thread 0 arrived
    at deneme2.Asallik.createThread(Asallik.java:32)
    at deneme2.Main.main(Main.java:19)
Java Result: 1
public-Asallik(字符串threadName){
//这个类扩展了Thread,所以您可以调用Thread方法
setName(threadName);
t2.加入(本条);
start();
//我把上面的评论改成了下面的评论
i++;
}
公共静态void createThread(){
str=新的ArrayList();
t2=新的ArrayList();
d=new ArrayList();//初始化d希望这有帮助

public class Test {

    private class MyRunnable implements Runnable {

        @Override
        public void run() {
            System.err.println("Thread name: " + Thread.currentThread().getName());
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public Test() {
        for (int i = 1; i <= 2; i++) {
            final int f = i;
            ThreadFactory factory = new ThreadFactory() {
                @Override
                public Thread newThread(Runnable r) {
                    return new Thread(r, "Thread Name " + f);
                }
            };
            Thread thread = factory.newThread(new MyRunnable());
            thread.start();
        }
    }

    public static void main(String[] args) {
        new Test();
        while (true) {
            // Wait all threads print their names
        }
    }

}
公共类测试{
私有类MyRunnable实现Runnable{
@凌驾
公开募捐{
System.err.println(“线程名称:”+Thread.currentThread().getName());
试一试{
睡眠(500);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
公开考试(){

对于(int i=1;i),您的d ArrayList为null。您必须对其进行初始化。此外,如果您正在扩展
Thread
,那么您应该改为实现Runnable。或者只需调用
setName(threadName);t2.add(this);start();
我现在已初始化。您能给我举个例子@WalterMI不明白为什么会有
而(true)
public Asallik(String threadName) {

    //this class extends Thread so you can call Thread methods
    setName(threadName);
    t2.add(this);
    start();
    //I changed everything above this comment to the next

    i++;
}

public static void createThread() {
    str = new ArrayList();
    t2 = new ArrayList();
    d = new ArrayList(); //initialize d <-------------------
    for (int i = 0; i < 2; i++) {
        str.add("Thread " + String.valueOf(i));
        d.add(new Asallik(str.get(i)));
    }
}
public class Test {

    private class MyRunnable implements Runnable {

        @Override
        public void run() {
            System.err.println("Thread name: " + Thread.currentThread().getName());
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public Test() {
        for (int i = 1; i <= 2; i++) {
            final int f = i;
            ThreadFactory factory = new ThreadFactory() {
                @Override
                public Thread newThread(Runnable r) {
                    return new Thread(r, "Thread Name " + f);
                }
            };
            Thread thread = factory.newThread(new MyRunnable());
            thread.start();
        }
    }

    public static void main(String[] args) {
        new Test();
        while (true) {
            // Wait all threads print their names
        }
    }

}