java:是线程中可见的全局变量 [英] java: are global variables visible in threads

查看:478
本文介绍了java:是线程中可见的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在主线程中声明一个全局变量,假设从主线程运行一个新线程,新线程是否可以访问主线程中的全局变量?

msg字符串是我的变量

  / *一个简单的横幅applet。 

这个小程序创建一个线程,用于在msg的右边滚动
消息,使其在applet的窗口中向右滚动

* /
import java.awt。*;
import java.applet。*;
/ *
< / applet>
* /

公共类AppletSkel扩展了Applet实现的Runnable {
String msg =一个简单的移动横幅。 //<< -----------------访问
的变量Thread t = null;
int状态;
boolean stopFlag;

//设置颜色并初始化线程。
public void init(){
setBackground(Color.cyan);
setForeground(Color.red);


//启动线程
public void start(){
t = new Thread(this);
stopFlag = false;
t.start();
}

//运行横幅的线程的入口点。
public void run(){
char ch;

//显示横幅
((;;)){
try {
repaint();
Thread.sleep(250);
ch = msg.charAt(0);
msg = msg.substring(1,msg.length());
msg + = ch;
if(stopFlag)
break;
} catch(InterruptedException e){}
}
}

//暂停横幅。
public void stop(){
stopFlag = true;
t = null;
}

//显示横幅。
public void paint(Graphics g){
g.drawString(msg,50,30);
g.drawString(msg,80,40);



$ div $解析方案

变量对于多个线程可见的通常是棘手的。但是,字符串是不可变的,因此简化了情况。



它是可见的,但是当其他线程可用的修改的普通值不能保证时。你应该使它 volatile ,这样它就不会在本地缓存线程。在分配 msg 之前使用局部变量来构建新字符串。

如果您打算修改 stopFlag 来自其他线程,它也应该是 volatile


if I declare a global variable in the main thread, suppose that from the main thread I run a new thread, can the new thread access the global variable in the main thread?

"msg" string is my variable to acces

/* A simple banner applet.

   This applet creates a thread that scrolls
   the message contained in msg right to left
   across the applet's window.
*/
import java.awt.*;
import java.applet.*;
/*
<applet code="SimpleBanner" width=300 height=50>
</applet>
*/

public class AppletSkel extends Applet implements Runnable {
  String msg = " A Simple Moving Banner.";  //<<-----------------VARIABLE TO ACCESS
  Thread t = null;
  int state;
  boolean stopFlag;

  // Set colors and initialize thread.
  public void init() {
    setBackground(Color.cyan);
    setForeground(Color.red);
  }

  // Start thread
  public void start() {
    t = new Thread(this);
    stopFlag = false;
    t.start();
  }

  // Entry point for the thread that runs the banner.
  public void run() {
    char ch;

    // Display banner 
    for( ; ; ) {
      try {
        repaint();
        Thread.sleep(250);
        ch = msg.charAt(0);
        msg = msg.substring(1, msg.length());
        msg += ch;
        if(stopFlag)
          break;
      } catch(InterruptedException e) {}
    }
  }

  // Pause the banner.
  public void stop() {
    stopFlag = true;
    t = null;
  }

  // Display the banner.
  public void paint(Graphics g) {
    g.drawString(msg, 50, 30);
    g.drawString(msg, 80, 40);
  }
}

解决方案

Variables that are visible to several threads are generally tricky. Strings, however, are immutable, so that simplifies the situation.

It is visible, but when a modified ordinary value is available to other threads is not guaranteed. You should make it volatile, so that it is not cached thread locally. Use a local variable to build the new string before assigning msg.

If you intend to modify stopFlag from other threads, it should also be volatile.

这篇关于java:是线程中可见的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆