打印数字,读取通知 (1)在main方法中启动两个线程 (2)在1个线程循环打印100以内的整数(3)直到第2个线程从键盘读取

//2、打印数字,读取通知
//        (1)在main方法中启动两个线程;
//        (2)在1个线程循环打印100以内的整数;
//        (3)直到第2个线程从键盘读取了“Q”命令。
public class Test2 {
    public static void main(String[] args) {
        MyThread1 thread1 =new MyThread1();
        MyThread2 thread2 =new MyThread2(thread1);
        Thread t1 = new Thread(thread1);
        Thread t2 = new Thread(thread2);
        t2.start();
        t1.start();

    }
}


import java.util.Random;

public class MyThread1 implements Runnable{
    public static boolean loop =true;
    @Override
    public void run() {
        Random r =new Random();

        while (loop){
            try {
                Thread.sleep(1000);
                System.out.println(r.nextInt(100));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }
}



import java.util.Scanner;

public class MyThread2 implements Runnable{
    private MyThread1 myThread1;

    public MyThread2(MyThread1 myThread1) {
        this.myThread1 = myThread1;
    }

    @Override
    public void run() {
        Scanner sc =new Scanner(System.in);
        while (true) {
            try {
                Thread.sleep(1);
                System.out.println("请输入您的指令:");
                String input= sc.nextLine();
                if(input.equals("q")){
                    myThread1.loop = false;
                    break;
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }
}

Java创建一个多线程程序,你可以使用`Thread`类或者更推荐的`ExecutorService`和`Callable`/`Future`来实现。这里是一个简单的示例,我们将使用`java.util.concurrent`包下的工具: ```java import java.io.*; import java.util.concurrent.*; public class MultiThreadedFileProcessor { private static final String FILE_X1 = "x1.txt"; private static final String FILE_X2 = "x2.txt"; private static final String FILE_X3 = "x3.txt"; public static void main(String[] args) throws IOException, InterruptedException { ExecutorService executor = Executors.newSingleThreadExecutor(); Future<String> future1 = executor.submit(new FileReaderAndWriter(FILE_X1, FILE_X3, 2000)); Future<String> future2 = executor.submit(new FileReaderAndWriter(FILE_X2, FILE_X3, 2000)); // 等待所有任务完成并关闭线程池 try { String result1 = future1.get(); // 获取第一个文件处理结果 String result2 = future2.get(); // 获取第二个文件处理结果 System.out.println("Results: " + result1 + ", " + result2); } catch (ExecutionException e) { e.printStackTrace(); } finally { executor.shutdown(); } } // 文件读取并乘以2后写入新文件的线程类 private static class FileReaderAndWriter implements Callable<String> { private String inputFile; private String outputFile; private long interval; public FileReaderAndWriter(String inputFile, String outputFile, long interval) { this.inputFile = inputFile; this.outputFile = outputFile; this.interval = interval; } @Override public String call() { try (BufferedReader reader = new BufferedReader(new FileReader(inputFile)); BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) { String line; while ((line = reader.readLine()) != null) { int number = Integer.parseInt(line); // 假设都是整数 int doubledNumber = number * 2; writer.write(doubledNumber + "\n"); // 写入新的数值 Thread.sleep(interval); // 暂停两秒 } return "Write to file " + outputFile + " completed."; } catch (IOException | InterruptedException e) { throw new RuntimeException(e); } } } } ``` 在这个例子,我们创建了一个单线程的`ExecutorService`,然后提交了两个任务(`future1`和`future2`),每个任务代表读取一个文件并每两秒写入到`FILE_X3`。注意,实际应用需要处理可能出现的异常,并在适当的时候关闭输入输出流。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值