多线程时如何在类之间发送变量
我的多线程项目的简化版本,它只是一种以一种简单的方式复制问题的方法
这是
。做是将一个从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
without multithreading
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用一个阻止标准,线程(生产者)将
将
添加到共享队列,而主(消费者)将在上阻止
takemain
开始
参见 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 thetake
main
startSession
see https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/BlockingQueue.html
and
https://jenkov.com/tutorials/java-util-concurrent/blockingqueue.html