在运行()方法中设置一个静态变量在java
Setting a static variable in a run () method in java
我有一个静态变量,我想在 运行() 方法中设置它。我有以下内容:
public class Test{
public static int temp;
public static void main(String [] args)
{
update();
System.out.println("This is the content of temp"+temp);
}
public static void update()
{
(new Thread() {
@Override
public void run() {
// do some stuff
Test.temp=15;
}}).start();
}
我想将temp的内容更新为15;但是当我在主函数中打印它时,它显示为 0。如何解决这个问题?
线程并发工作,因此您应该等到新线程完成:
public class Test{
public static int temp;
public static void main(String [] args) {
update().join(); //we wait until new thread finishes
System.out.println("This is the content of temp"+temp);
}
public static Thread update() {
Thread t = new Thread() {
@Override
public void run() {
// do some stuff
Test.temp=15;
}
};
t.start();
return t;
}
你必须了解 Thread 的工作原理。
这里先给大家看两段代码是为了理解,在线程内部初始化的变量需要时间更新,直到线程结束。
public class Num {
public static int temp;
public static void main(String [] args) throws InterruptedException
{
update();
System.out.println("This is the content of temp"+Num.temp);//This will print before temp=15 is updated
}
public static void update()
{
(new Thread() {
@Override
public void run() {
// do some stuff
Num.temp=15;
System.out.println("Value of temp:"+Num.temp);//This statement prints after
}}).start();
}
}
它打印以下内容:
This is the content of temp0
Value of temp:15
第二个显示,如果你在线程执行后等待一小段时间(Thread.sleep(10)),值会更新:
public class Num {
public static int temp;
public static void main(String [] args) throws InterruptedException
{
update();
Thread.sleep(10);
System.out.println("This is the content of temp"+Num.temp);//This will print correct value now
}
public static void update()
{
(new Thread() {
@Override
public void run() {
// do some stuff
Num.temp=15;
}}).start();
}
}
但在这里我建议使用与 Philip 相同的方法。只需在主函数
中添加throws InterruptedException
我有一个静态变量,我想在 运行() 方法中设置它。我有以下内容:
public class Test{
public static int temp;
public static void main(String [] args)
{
update();
System.out.println("This is the content of temp"+temp);
}
public static void update()
{
(new Thread() {
@Override
public void run() {
// do some stuff
Test.temp=15;
}}).start();
}
我想将temp的内容更新为15;但是当我在主函数中打印它时,它显示为 0。如何解决这个问题?
线程并发工作,因此您应该等到新线程完成:
public class Test{
public static int temp;
public static void main(String [] args) {
update().join(); //we wait until new thread finishes
System.out.println("This is the content of temp"+temp);
}
public static Thread update() {
Thread t = new Thread() {
@Override
public void run() {
// do some stuff
Test.temp=15;
}
};
t.start();
return t;
}
你必须了解 Thread 的工作原理。
这里先给大家看两段代码是为了理解,在线程内部初始化的变量需要时间更新,直到线程结束。
public class Num {
public static int temp;
public static void main(String [] args) throws InterruptedException
{
update();
System.out.println("This is the content of temp"+Num.temp);//This will print before temp=15 is updated
}
public static void update()
{
(new Thread() {
@Override
public void run() {
// do some stuff
Num.temp=15;
System.out.println("Value of temp:"+Num.temp);//This statement prints after
}}).start();
}
}
它打印以下内容:
This is the content of temp0
Value of temp:15
第二个显示,如果你在线程执行后等待一小段时间(Thread.sleep(10)),值会更新:
public class Num {
public static int temp;
public static void main(String [] args) throws InterruptedException
{
update();
Thread.sleep(10);
System.out.println("This is the content of temp"+Num.temp);//This will print correct value now
}
public static void update()
{
(new Thread() {
@Override
public void run() {
// do some stuff
Num.temp=15;
}}).start();
}
}
但在这里我建议使用与 Philip 相同的方法。只需在主函数
中添加throws InterruptedException