Java,如何在TextArea中使用多线程?我需要同步线程吗?
In Java, how do I use multithreading in TextArea? Do I need to synchronize my threads?
我对 Java 和多线程没有经验,所以也许你们可以提供帮助。当我多线程时,我无法在 TextArea 框中打印出一系列数字和字母。这是我的代码:
public class MultiThread extends Application {
static TextArea outputArea = new TextArea();
@Override
public void start(Stage primaryStage) throws Exception {
outputArea.setWrapText(true);
Runnable printA = new PrintChar('a', 100);
Runnable printB = new PrintChar('b', 100);
Runnable print100 = new PrintNum(100);
// Create threads
Thread thread1 = new Thread(printA);
Thread thread2 = new Thread(printB);
Thread thread3 = new Thread(print100);
thread1.start();
thread2.start();
thread3.start();
Scene scene = new Scene(outputArea, 250, 130);
primaryStage.setTitle("Concurrent Output");
primaryStage.setScene(scene);
primaryStage.show();}
public static void main(String[] args) {
launch(args);
}
}
class PrintChar extends MultiThread implements Runnable {
private char charToPrint; // The character to print
private int times; // The times to repeat
public PrintChar(char c, int t) {
charToPrint = c;
times = t;
}
@Override
public void run() {
for (int i = 0; i < times; i++) {
outputArea.appendText(charToPrint + "");
}
}
}
class PrintNum extends MultiThread implements Runnable {
private int lastNum;
private char charToPrint;
public PrintNum(int n) {
lastNum = n;
}
@Override
public void run() {
for (int i = 1; i <= lastNum; i++) {
outputArea.appendText(i + "");
}
}
}
当我 运行 代码时,线程通常不会打印出它们应该打印的所有内容。例如,'print100' 并不总是像预期的那样打印到数字 100。我也得到随机结果,每次都不会得到相同的输出或错误。
Exception in thread "Thread-6" Exception in thread "Thread-4" java.lang.IndexOutOfBoundsException
at javafx.scene.control.TextInputControl.getText(TextInputControl.java:451)
at javafx.scene.control.TextInputControl.updateContent(TextInputControl.java:564)
at javafx.scene.control.TextInputControl.replaceText(TextInputControl.java:548)
at javafx.scene.control.TextInputControl.insertText(TextInputControl.java:473)
at javafx.scene.control.TextInputControl.appendText(TextInputControl.java:463)
at threader.PrintChar.run(Threader.java:53)
at java.lang.Thread.run(Thread.java:748)
java.lang.IndexOutOfBoundsException
at javafx.scene.control.TextInputControl.getText(TextInputControl.java:451)
at javafx.scene.control.TextInputControl.updateContent(TextInputControl.java:555)
at javafx.scene.control.TextInputControl.replaceText(TextInputControl.java:548)
at javafx.scene.control.TextInputControl.insertText(TextInputControl.java:473)
at javafx.scene.control.TextInputControl.appendText(TextInputControl.java:463)
at threader.PrintNum.run(Threader.java:71)
at java.lang.Thread.run(Thread.java:748)
我几乎可以肯定这是在代码中同步多个线程的问题,但我似乎无法弄清楚如何针对此特定代码执行此操作。
TextArea
实现显然不是线程安全的,因此您应该确保在 TextArea
对象上调用方法时不会中断当前线程。例如,您可以通过同步 TextArea
对象本身来执行此操作:
synchronized (outputArea) {
outputArea.appendText(charToPrint + "");
}
在MultiThread
class中创建一个静态函数如下:
public static synchronized setText(String str) {
outputArea.appendText(str);
}
并将行 outputArea.appendText(...);
替换为 MultiThread.setText(...)
。
问题:您目前正在从三个线程设置 outputArea 上的文本,并且它们同时都试图在 outputArea 上附加文本。因此,用于附加文本的 outputArea 中的索引与另一个线程重叠。
解决方案:确保只有一个线程尝试在 outputArea 上追加文本。
我对 Java 和多线程没有经验,所以也许你们可以提供帮助。当我多线程时,我无法在 TextArea 框中打印出一系列数字和字母。这是我的代码:
public class MultiThread extends Application {
static TextArea outputArea = new TextArea();
@Override
public void start(Stage primaryStage) throws Exception {
outputArea.setWrapText(true);
Runnable printA = new PrintChar('a', 100);
Runnable printB = new PrintChar('b', 100);
Runnable print100 = new PrintNum(100);
// Create threads
Thread thread1 = new Thread(printA);
Thread thread2 = new Thread(printB);
Thread thread3 = new Thread(print100);
thread1.start();
thread2.start();
thread3.start();
Scene scene = new Scene(outputArea, 250, 130);
primaryStage.setTitle("Concurrent Output");
primaryStage.setScene(scene);
primaryStage.show();}
public static void main(String[] args) {
launch(args);
}
}
class PrintChar extends MultiThread implements Runnable {
private char charToPrint; // The character to print
private int times; // The times to repeat
public PrintChar(char c, int t) {
charToPrint = c;
times = t;
}
@Override
public void run() {
for (int i = 0; i < times; i++) {
outputArea.appendText(charToPrint + "");
}
}
}
class PrintNum extends MultiThread implements Runnable {
private int lastNum;
private char charToPrint;
public PrintNum(int n) {
lastNum = n;
}
@Override
public void run() {
for (int i = 1; i <= lastNum; i++) {
outputArea.appendText(i + "");
}
}
}
当我 运行 代码时,线程通常不会打印出它们应该打印的所有内容。例如,'print100' 并不总是像预期的那样打印到数字 100。我也得到随机结果,每次都不会得到相同的输出或错误。
Exception in thread "Thread-6" Exception in thread "Thread-4" java.lang.IndexOutOfBoundsException
at javafx.scene.control.TextInputControl.getText(TextInputControl.java:451)
at javafx.scene.control.TextInputControl.updateContent(TextInputControl.java:564)
at javafx.scene.control.TextInputControl.replaceText(TextInputControl.java:548)
at javafx.scene.control.TextInputControl.insertText(TextInputControl.java:473)
at javafx.scene.control.TextInputControl.appendText(TextInputControl.java:463)
at threader.PrintChar.run(Threader.java:53)
at java.lang.Thread.run(Thread.java:748)
java.lang.IndexOutOfBoundsException
at javafx.scene.control.TextInputControl.getText(TextInputControl.java:451)
at javafx.scene.control.TextInputControl.updateContent(TextInputControl.java:555)
at javafx.scene.control.TextInputControl.replaceText(TextInputControl.java:548)
at javafx.scene.control.TextInputControl.insertText(TextInputControl.java:473)
at javafx.scene.control.TextInputControl.appendText(TextInputControl.java:463)
at threader.PrintNum.run(Threader.java:71)
at java.lang.Thread.run(Thread.java:748)
我几乎可以肯定这是在代码中同步多个线程的问题,但我似乎无法弄清楚如何针对此特定代码执行此操作。
TextArea
实现显然不是线程安全的,因此您应该确保在 TextArea
对象上调用方法时不会中断当前线程。例如,您可以通过同步 TextArea
对象本身来执行此操作:
synchronized (outputArea) {
outputArea.appendText(charToPrint + "");
}
在MultiThread
class中创建一个静态函数如下:
public static synchronized setText(String str) {
outputArea.appendText(str);
}
并将行 outputArea.appendText(...);
替换为 MultiThread.setText(...)
。
问题:您目前正在从三个线程设置 outputArea 上的文本,并且它们同时都试图在 outputArea 上附加文本。因此,用于附加文本的 outputArea 中的索引与另一个线程重叠。
解决方案:确保只有一个线程尝试在 outputArea 上追加文本。