通过annomyous内部类创建线程

我正在开发创建线程的代码,但没有扩展线程类或实现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(); } } 

现在请告诉我,我也可以用同样的方法创建子线程。!! 我试过的是……

 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()方法,我认为这不实用..请指教..!

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

不要等到一个人在开始第二个之前完成,或者你有三个线程在一个人运行的时候(在这种情况下,额外的线程毫无意义)

顺便说一句:你的synchronized没有做任何有用的事情,但它可能导致Thread无法正常工作。


您声明两个扩展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来处理线程策略。

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

创建匿名内部类的唯一两种方法之一是扩展Thread类或其任何子类(另一种是实现Runnable接口或其任何子类型)。 在第一段代码中,您扩展了线程类。 这就是为什么你把类名称作为MyThread3 $ 1(因为它是Anonymous内部类)和超类作为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将以其id打印1到20,Thread2将以其id打印50到70,这意味着每个线程正在执行自己的run方法。 注意:如果遇到th.start方法,主线程将逐行执行程序,然后主线程将发送子线程执行其run方法,主线程转到下一行执行。