Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过注释性内部类创建线程_Java - Fatal编程技术网

Java 通过注释性内部类创建线程

Java 通过注释性内部类创建线程,java,Java,我正在开发创建线程的代码,但没有扩展thread类或实现runnable接口,即通过匿名内部类 public class Mythread3 { public static void main(String... a) { Thread th = new Thread() { public synchronized void run() { for (int i = 0; i < 20; i++) {

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

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

    }
}
公共类Mythread3{
公共静态void main(字符串…a){
Thread th=新线程(){
公共同步的无效运行(){
对于(int i=0;i<20;i++){
试一试{
睡眠(1000);
系统输出打印(i+“\n+”);
}捕获(例外e){
e、 printStackTrace();
}
}
}
};
th.start();
螺纹y=新螺纹();
y、 start();
}
}
现在,请告诉我,我可以用同样的方法创建子线程吗。。!!我试过的是

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

    }
}
公共类Mythread3{
公共静态void main(字符串…a){
Thread th=新线程(){
公共同步的无效运行(){
对于(int i=0;i<20;i++){
试一试{
睡眠(1000);
系统输出打印(i+“\n+”);
}捕获(例外e){
e、 printStackTrace();
}
}
}
};
线程th1=新线程(){
公共同步的无效运行(){
对于(int i=0;i<20;i++){
试一试{
睡眠(1000);
系统输出打印(i+“\n+”);
}捕获(例外e){
e、 printStackTrace();
}
}
}
};
th.start();
试一试{
th.join();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
th1.start();
}
}
但是其中有两个run()方法,我认为这不实用..请建议

Runnable run=new Runnable(){
 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();
公开募捐{ 试一试{ 对于(int i=0;i<20;i++){ 睡眠(1000); 系统输出打印(i+“\n+”); } }捕捉(中断异常e){ 系统输出打印项次(“中断”); } } }; 新线程(run.start(); 新线程(run.start();
不要等到一个线程完成后再开始第二个线程,否则您有三个线程在其中一个线程上运行(在这种情况下,额外的线程是没有意义的)

顺便说一句:你的synchronized没有做任何有用的事情,但它可能会导致线程运行不正确



声明两个匿名内部类来扩展线程并重写run()方法本身并不是问题。我们可能认为不太可读,但没有问题。

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

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
    }
}
例如,这允许您多次启动同一作业


如果您想进一步了解,可以考虑使用线程策略。

< P>匿名内部类是没有名称的类,意味着它没有显式名称,但是JVM将其命名为MythRead 1美元,用于引用其对象。因此,当您打印th.getClass()和th.getClass().getSuperclass()时,您将得到MyThread3$1和Thread的输出

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

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

不清楚您试图执行什么,但您正在扩展线程(最好是实现Runnable)。您正在使用匿名内部类半隐式地执行此操作,但您仍然在执行此操作。现在不必要的头痛,明天可能需要大量咖啡因:-)我正在尝试创建子线程的内容也是。。!!请告知上述方法是否正确。。!1@JonSkeet请立即提出建议!:)
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