gpt4 book ai didi

java - 如何让2个线程共享数据? ( java )

转载 作者:行者123 更新时间:2023-11-30 01:55:45 24 4
gpt4 key购买 nike

我目前正在练习线程,因此我要求自己编写一个创建 2 个线程的程序。第一个线程将无休止地打印一个字符,第二个线程将无休止地等待输入,然后将其传递给第一个线程。然后线程 #1 应该打印传递的字符。这是我写的:

public class A extends Thread {
public char dif;
Scanner stdin = new Scanner(System.in);
@Override
public void run() {
for (; ; ) {
dif = stdin.nextLine().charAt(0);
MyThread.setCh(dif);
}
}
}

该线程接受输入,然后将其传递给此线程:

public class MyThread extends Thread {
public static char ch;
public static void setCh(char cha) {
ch = cha;
}
public static char getCh() {
return ch;
}
@Override
public void run() {
for(;;) {
try {
Thread.sleep(300);
}
catch(InterruptedException e) {
e.printStackTrace();
}
System.out.print(getCh());
}
}
}

main() 中发生了什么:

MyThread endless = new MyThread();
MyThread.setCh('$');
A set = new A();
endless.start();
set.start();

但是,这并没有按预期工作。无论我输入什么,程序都会不断打印$。另外,由于某种原因,当我第一次输入字符时,我收到了越界异常。

最佳答案

解决此问题的最简单方法可能是使用 BlockingQueue .

实际上,在您的示例中,从 System.in 接收字符的线程是生产者,打印接收到的字符的线程是消费者.

所以,这是实现您目标的代码:

import java.util.*;
import java.util.concurrent.*;

class Setup {
public static void main(String[] args) {
BlockingQueue<Character> q = new LinkedBlockingQueue<>();
Producer p = new Producer(q);
Consumer c = new Consumer(q);
new Thread(p).start();
new Thread(c).start();
}
}

class Producer implements Runnable {
private final BlockingQueue<Character> queue;
private final Scanner scanner = new Scanner(System.in);
Producer(BlockingQueue<Character> q) { queue = q; }
public void run() {
try {
while (true) {
queue.put(produce());
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt(); // set interrupt flag
} finally {
scanner.close();
}
}
Character produce() {
return scanner.nextLine().charAt(0);
}
}

class Consumer implements Runnable {
private final BlockingQueue<Character> queue;
Consumer(BlockingQueue<Character> q) { queue = q; }
public void run() {
try {
while (true) {
consume(queue.take());
}
} catch (InterruptedException ex) {}
}
void consume(Character c) {
System.out.println("Received character: " + c);
}
}

关于java - 如何让2个线程共享数据? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54581035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com