本文介绍了通过annomyous内部类创建线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发创建线程的代码,但没有扩展线程类或实现runnable接口,即通过匿名内部类..

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

    }
}

但有两个run()其中的方法,我认为这不实用..请指教..!

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

推荐答案

 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:你的synchronized没有做任何有用的事情,但它可能导致线程无法正常工作。

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

这篇关于通过annomyous内部类创建线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:22