我目前正在使用一种方法从剪贴板中以字符串形式获取数据,但是我需要从在不同线程上运行的另一种方法访问此信息,我已经进行了一些研究并且遇到了易失性字符串,但我不太了解确定如何在我的代码中实现它们,这是我代码的基础:
public class MobileSite {
public MobileSite(){
Thread thread = new Thread1(() -> {
try {
method1();
} catch (Exception botFailed) {
System.out.println("Bot Failed");
}
});
Thread thread = new Thread2(() -> {
try {
method2();
} catch (Exception botFailed) {
System.out.println("Bot Failed");
}
});
thread1.start();
thread2.start();
方法1获取数据,如果有人有任何建议,方法2需要使用字符串格式的数据
答案 0 :(得分:0)
我希望您使用StringBuilder而不是使用String。下面是工作示例
StringBuilder txt = new StringBuilder();
public void method1() {
txt.append("String assigned");
}
public void method2() {
System.out.println(txt.toString());
}
public MobileSite(){
Thread thread1 = new Thread(() -> {
try {
method1();
} catch (Exception botFailed) {
System.out.println("Bot Failed");
}
});
Thread thread2 = new Thread(() -> {
try {
method2();
} catch (Exception botFailed) {
System.out.println("Bot Failed");
}
});
thread1.start();
thread2.start();
}