java中的outputarea,在Java中,如何在TextArea中使用多线程?我需要同步我的线程吗?...

I am rather inexperienced with Java and multi-threading so maybe you guys can help. I am having trouble printing out a series of numbers and letters in a TextArea box when I multi-thread. This is my code:

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 + "");

}

}

}

When I run the code, the threads do not usually print out all of what they are supposed to. For example, 'print100' does not always print to number 100 like it's supposed to. I also get random results and I never get the same output nor errors each time.

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)

I am almost positive it is a problem with synchronizing the multiple threads in the code, but I can't seem to figure out how to do so for this specific code.

解决方案

The TextArea implementation is obviously not thread safe, so you should make sure that the current thread is not interupted while calling methods on your TextArea object. You can do this for example by synchronizing on the TextArea object itself:

synchronized (outputArea) {

outputArea.appendText(charToPrint + "");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值