多线程时如何在类之间发送变量

发布于 2025-02-12 07:03:37 字数 1720 浏览 0 评论 0原文

我的多线程项目的简化版本,它只是一种以一种简单的方式复制问题的方法

这是

。做是将一个从startsession.java发送到main.java的变量,并且我也使用多线程。但是,我面临的问题是,每次我尝试检索Main内部的变量时,我都会获得无效的值。

内部开始有运行方法和setter(setSessionKey(string sess))和getter(getsessionkey())方法。我为测试变量进行了硬编码。

GET方法仅在运行方法中起作用,但是当我从Main内部调用GetSessionKey()时,我会如下所示。但是,当我使用多线程时,这只是一个问题。当我不使用多线程,而只需从内部主机调用运行方法时,要变量IM的寻找不再为空。

我的问题是否有办法在使用多线程时将变量从Startessession发送到Main?

谢谢

startsession.java

public class startSession extends Thread {

    static String sessionKey;

    public void run() {
        String createdSession = "83248329";
        setSessionKey(createdSession);
        System.out.println("Inside run method: " + getSessionKey());
    }

    public String getSessionKey() {
        return sessionKey;
    }

    public void setSessionKey(String sess) {
        sessionKey = sess;
    }
}

main.java

package com.Server;

public class Main {

    static String session;

    public static void main(String[] args) throws InterruptedException {
        startSession startSession = new startSession();

        startSession.start();

        session = startSession.getSessionKey();
        System.out.println("Inside Main: " + session);
    }
}

带有多线程

”输入图像描述在这里“

没有多线程

”在此处输入图像描述”

This a much simplified version of my multithreading project and its just a way to replicate the issue in a simpler way for easy understanding.

So I have two classes startSession.java and main.java

what I am trying to do is to send a variable from startSession.java to main.java and Im also using multithreading. However, the problem I am facing is that everytime I try to retrieve the variable inside main I get a null value.

Inside startSession theres the run method and Setter(setSessionKey(String sess)) and getter(getSessionKey()) methods. I hardcoded a variable to test.

The get method only works inside the run method but when I call getSessionKey() from inside main I get a null as seen below. However, this is only a problem when I am using multithreading. When I dont use multithreading and instead just call the run method from inside main, the variable Im looking for is no longer null.

My question is there a way to send a variable from startSession to main while using multithreading ?

thank you

startSession.java

public class startSession extends Thread {

    static String sessionKey;

    public void run() {
        String createdSession = "83248329";
        setSessionKey(createdSession);
        System.out.println("Inside run method: " + getSessionKey());
    }

    public String getSessionKey() {
        return sessionKey;
    }

    public void setSessionKey(String sess) {
        sessionKey = sess;
    }
}

main.java

package com.Server;

public class Main {

    static String session;

    public static void main(String[] args) throws InterruptedException {
        startSession startSession = new startSession();

        startSession.start();

        session = startSession.getSessionKey();
        System.out.println("Inside Main: " + session);
    }
}

with multithreading

enter image description here

without multithreading

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日裸衫吸 2025-02-19 07:03:38

使用一个阻止标准,线程(生产者)将添加到共享队列,而主(消费者)将在上阻止 take

main

 public static void main(String[] args) throws Exception {

    // example only uses 1024 - check what is best for you
    BlockingQueue queue = new ArrayBlockingQueue(1024);

    StartSession producer = new StartSession(queue);

    ....
    System.out.println(queue.take());

开始

   String createdSession= "83248329";
   queue.add(createdSession);

参见 https://docs.oracle.com/javase/8/docs/api/java/java/util/concurrent/concurrent/blockingqueue.html


https://jenkov.com/tutorials/java-util-concurrent/blockingqueue. html

Use a BlockingQueue whereby the Thread (Producer) will add to the shared queue and the Main (Consumer) will block on the take

main

 public static void main(String[] args) throws Exception {

    // example only uses 1024 - check what is best for you
    BlockingQueue queue = new ArrayBlockingQueue(1024);

    StartSession producer = new StartSession(queue);

    ....
    System.out.println(queue.take());

startSession

   String createdSession= "83248329";
   queue.add(createdSession);

see https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/BlockingQueue.html

and
https://jenkov.com/tutorials/java-util-concurrent/blockingqueue.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文