如何在方法和线程之间共享字符串?

问题描述 投票:0回答:1

我目前正在使用一种方法以字符串形式从剪贴板获取数据,但是我需要从在不同线程上运行的另一种方法访问此信息,我已经进行了一些研究并且遇到了易失性字符串,但是我不确定如何在我的代码中实现它们,这是我代码的基础:

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需要使用字符串格式的数据,]

我目前正在使用一种方法以字符串形式从剪贴板获取数据,但是我需要从在不同线程上运行的另一种方法访问此信息,我已经做了一些研究,并且...

java string multithreading selenium volatile
1个回答
0
投票
我希望您使用StringBuilder而不是使用String。以下是工作示例
© www.soinside.com 2019 - 2024. All rights reserved.