4

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..!

4

3 回答 3

10
 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();

在开始第二个之前不要等待一个完成,否则你有三个线程,其中一个正在运行(在这种情况下,额外的线程毫无意义)

顺便说一句:您的同步没有做任何有用的事情,但它可能导致线程无法正常运行。


于 2012-04-17T07:10:18.443 回答
1

您声明两个匿名内部类扩展 Thread 并覆盖 run() 方法这一事实本身并不是问题。我们可能认为不是真正可读但没有问题。

但是,您应该考虑使用该Runnable接口。您应该将处理/算法和线程策略分开。所以最好有这样的东西:

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

例如,这允许您多次启动同一个作业。

如果你想更进一步,你可以考虑使用ThreadPoolExecutor来处理你的线程策略。

于 2012-04-17T07:16:38.357 回答
1

匿名内部类是没有名称的类,意味着它没有明确的名称,但 JVM 将其命名为 Mythread3$1 以引用其对象。因此,当您打印 th.getClass() 和 th.getClass().getSuperclass() 时,您将获得 MyThread3$1 和 Thread 的输出。

您可以通过扩展 Thread 类或其任何子类(另一个实现 Runnable 接口或其任何子类型)来创建匿名内部类。在第一段代码中,您扩展了线程类。这就是为什么您将类名命名为 MyThread3$1(因为它是匿名内部类)和超类作为 Thread 类(当您扩展它时)。所以你可以创建尽可能多的匿名内部类扩展一个线程类,JVM将它们命名为 MyThread3$1、MyThread3$2、MyThread3$3...... 但是当你调用 start 方法时,每个线程将只执行它们的 run 方法(你覆盖了在 MyThread3$1, MyThread3$2 通过扩展线程类)。

package com.tej.threads;

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 {

                    System.out.println(i + "\t" + ".." + Thread.currentThread().getId());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

    };
    Thread th1 = new Thread() {

        public synchronized void run() {
            for (int i = 50; i < 70; i++) {
                try {

                    System.out.println(i + "\t" + ".."+ Thread.currentThread().getId());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

    };
    th.start();
    try {
        th.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    th1.start();
    System.out.println(th.getClass()+ " " + th.getClass().getSuperclass());
    for(int i=0;i<20;i++)
    {
        System.out.println("i am main Thread");
    }


  }
}

输出是

0   ..8
1   ..8
2   ..8
3   ..8
4   ..8
5   ..8
6   ..8
7   ..8
8   ..8
9   ..8
10  ..8
11  ..8
12  ..8
13  ..8
14  ..8
15  ..8
16  ..8
17  ..8
18  ..8
19  ..8
class com.tej.threads.MyThread3$1 class java.lang.Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread  
i am main Thread
i am main Thread 
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
50  ..9
51  ..9
52  ..9
53  ..9
54  ..9
55  ..9
56  ..9
57  ..9
58  ..9
59  ..9
60  ..9
61  ..9
62  ..9
63  ..9
64  ..9
65  ..9
66  ..9
67  ..9
68  ..9
69  ..9

您可以清楚地看到 thread1 将打印 1 到 20 及其 id,Thread2 将打印 50 到 70 及其 id,这意味着每个线程都在执行自己的 run 方法。 注意:主线程如果遇到th.start方法会逐行执行程序,然后主线程将发送子线程执行其run方法,主线程进入下一行执行。

于 2017-10-04T11:02:53.027 回答