一个接口多个实现类,循环执行每个实现类

1.引用依赖

<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.9.11</version>
</dependency>

2.编写一个接口

public interface UseSearch {
    public String useSearch();
}

3.两个实现类

public class UseSearch1 implements UseSearch {
    @Override
    public String useSearch() {
        return "类1";
    }
}

public class UseSearch2 implements UseSearch {

    @Override
    public String useSearch() {
        return "类2";
    }
}

4.反射找到接口下所有的类,并执行它

public String test() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
        List<String> returnValue=new ArrayList<>();
        String packageName="com.demo";
        Reflections reflections = new Reflections(packageName);
        Set<Class<? extends UseSearch>> monitorClasses = reflections.getSubTypesOf(UseSearch.class);
        for(Class<? extends UseSearch> monitor:monitorClasses)
        {
            Class useClass = Class.forName(monitor.getName());
            UseSearch use= (UseSearch)useClass.newInstance();
            String result = use.useSearch();
            //循环执行类
            returnValue.add(result);
        }
        return returnValue.stream().collect(Collectors.joining(","));
    }

5.测试一下

public static void main(String[] args) {
        UseTest mainTest=new UseTest();
        try {
            System.out.println(mainTest.test());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

执行效果如下:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以使用以下代码实现Java循环创建线程,并在每个线程中捕获异常: ``` public class LoopThread { public static void main(String[] args) { for (int i = 0; i < 5; i++) { new Thread(() -> { try { // 线程代码 } catch (Exception e) { e.printStackTrace(); } }).start(); } } } ``` 在上面的代码中,我们使用了一个循环语句创建了 5 个线程。每个线程都在一个 try-catch 语句块中运行,以便捕获可能发生的异常。 ### 回答2: 在Java中,可以使用如下的代码实现循环创建线程,并在每个线程中捕获异常: ```java public class ThreadCreationDemo { public static void main(String[] args) { int numThreads = 10; // 要创建的线程数量 for (int i = 0; i < numThreads; i++) { Thread thread = new Thread(new MyRunnable()); thread.start(); } } static class MyRunnable implements Runnable { @Override public void run() { try { // 在这里编写线程的具体逻辑 // 可能会抛出异常的代码 } catch (Exception e) { // 捕获异常,并处理异常 System.out.println("线程发生异常:" + e.getMessage()); } } } } ``` 在上面的代码中,`ThreadCreationDemo`的`main`方法中,首先定义了要创建的线程数量`numThreads`为10。 然后,在循环中,通过创建`Thread`对象,并传入一个实现了`Runnable`接口的`MyRunnable`对象,来创建线程。每个线程都会启动并执行`MyRunnable`的`run`方法。 在`MyRunnable`的`run`方法中,我们编写了具体的线程逻辑。在逻辑中的可能会抛出异常的代码处,我们使用`try-catch`语句来捕获异常。当异常被捕获时,会执行`catch`块中的代码,这里我们简单地打印出异常信息。 通过这种方式,我们可以循环创建多个线程,并且每个线程都可以捕获异常。当然,在实际的应用中,可能会根据具体需求做一些额外的处理。 ### 回答3: Java实现循环创建线程并捕获异常的方法如下: ```java public class ThreadCreationWithExceptionHandler { public static void main(String[] args) { final int numThreads = 5; for (int i = 0; i < numThreads; i++) { Thread thread = new Thread(() -> { try { // 你的线程逻辑代码 System.out.println("线程" + Thread.currentThread().getId() + "在执行"); } catch (Exception e) { // 捕获异常并进行处理 System.out.println("线程" + Thread.currentThread().getId() + "捕获到异常:" + e.getMessage()); } }); thread.start(); try { // 睡眠一段时间,确保上一线程已经启动 Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } ``` 上述代码中,我们使用循环创建了5个线程。在线程的Runnable实现中,我们捕获了所有的异常,并进行了处理。通过调用`Thread.currentThread().getId()`方法,我们可以获取当前线程的ID,方便在日志中区分不同的线程。为了确保每个线程都能独立执行,我们在创建线程后使用`Thread.sleep()`方法进行了短暂的睡眠。 希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值