-2

I'm new at performing Multi Threading.

I want to 2 thread with different name. But it is not gonna happen. It takes the last one for the name.

Here is my class' code which extended Thread :

    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);
            }
        }

    }

here is main code :

package deneme2;

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {        
        Asallik.createThread();
    }
}

I tried this way but I couldn't make it. Is there a way for performing multi threading in one Run Method but different names ?

Here is the result :

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
Musti
  • 27
  • 4

2 Answers2

0
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)));
    }
}
WalterM
  • 2,686
  • 1
  • 19
  • 26
0

Hope this help

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
        }
    }

}