Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating threads through annomyous inner class

Tags:

java

I was developing the code of creating a thread but without extending the thread class or implementing the runnable interface , that is through anonymous inner classes ..

public class Mythread3 {
    public static void main(String... a) {

        Thread th = new Thread() {

            public synchronized void run() {
                for (int i = 0; i < 20; i++) {
                    try {
                        Thread.sleep(1000);

                        System.out.print(i + "\n" + "..");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }

        };

        th.start();
        Thread y = new Thread();
        y.start();

    }
}

Now please advise me can I create child threads also with the same approach..!! what I have tried is that...

public class Mythread3 {
    public static void main(String... a) {

        Thread th = new Thread() {

            public synchronized void run() {
                for (int i = 0; i < 20; i++) {
                    try {
                        Thread.sleep(1000);

                        System.out.print(i + "\n" + "..");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }

        };

        Thread th1 = new Thread() {

            public synchronized void run() {
                for (int i = 0; i < 20; i++) {
                    try {
                        Thread.sleep(1000);

                        System.out.print(i + "\n" + "..");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }

        };
        th.start();
        try {
            th.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        th1.start();

    }
}

But there are two run() methods in it, I think this not practical..please advise..!

like image 706
dghtr Avatar asked Dec 16 '22 01:12

dghtr


2 Answers

 Runnable run = new Runnable() {
    public void run() {
        try {
            for (int i = 0; i < 20; i++) {
                Thread.sleep(1000);
                System.out.print(i + "\n" + "..");
            }

        } catch (InterruptedException e) {
            System.out.println(" interrupted");
        }
    }
 };
 new Thread(run).start();
 new Thread(run).start();

Don't wait for one to finish before starting the second or you have three threads where on one is ever running (in which case the additional threads were pointless)

BTW: Your synchronized isn't doing anything useful but it could cause the Thread to function incorrectly.


like image 51
Peter Lawrey Avatar answered Dec 21 '22 23:12

Peter Lawrey


The fact that you declare two anonymous inner classes extending Thread and overriding the run() method is not an issue in itself. We may consider not really readable but there is no issue.

However, you should consider using the Runnable interface. You should separate the processing/algorithms and the Threading policy. So it would be better to have something like this:

public class ThreadLauncher {
    public static void main(String[] args) {
         Thread job1 = new Thread(new Job1());
         Thread job2 = new Thread(new Job2());
         job1.start();
         job2.start();
    }
}

public class Job1 implements Runnable {
    @Override
    public void run() {
         // Do some stuff
    }
}

public class Job2 implements Runnable {
    @Override
    public void run() {
         // Do some other stuff
    }
}

This allows you to launch several time the same job, for example.

If you want to take it one step further, you could consider using ThreadPoolExecutor to handle your Threading strategy.

like image 20
Guillaume Polet Avatar answered Dec 22 '22 00:12

Guillaume Polet