刚开始使用 java 中的线程,我无法解释程序的输出
public class ThreadExample extends Thread{
private int info;
static int x = 0;
public ThreadExample (int info) {
this.info = info;
}
public void run () {
if ( info == 1 ) {
x = 3;
System.out.println(Thread.currentThread().getName() + " " + x);
} else{
x = 1;
System.out.println(Thread.currentThread().getName() + " " + x);
}
}
public static void main (String args []) {
ThreadExample aT1 = new ThreadExample(1);
ThreadExample aT2 = new ThreadExample(2);
aT1.start();
aT2.start();
System.err.println(x); …Run Code Online (Sandbox Code Playgroud) java multithreading static-variables thread-sleep java-threads
class a
{
int variable;//4 bytes
}
class a
{
static int variable;//? bytes
}
Run Code Online (Sandbox Code Playgroud) 请考虑以下示例代码:
File1.cpp
#include <iostream>
static int x = 6; // line 3
int main()
{
int x = 10; // line 7
{
extern int x; // line 9
x = x + 5;
std::cout << "x = " << x << "\n";
}
}
Run Code Online (Sandbox Code Playgroud)
由于外部声明x,将访问静态变量x而不是自动变量x.但我得到以下编译器警告:
File1.cpp:5:警告:'x'已定义但未使用
即使有了这个警告,我也得到了预期的输出x = 11,即5增加到静态变量的值x.
为什么编译器会给出上述警告?我正在使用GCC版本g++ (GCC) 3.4.6
考虑以下代码:
class A {
static int i=3;
}
public class TT extends A {
public static void main(String[] args) {
System.out.println(new A().getClass().i);
}
}
Run Code Online (Sandbox Code Playgroud)
该getClass()方法可以用于访问此上下文中的静态变量吗?
我想知道如何在java中实现本地静态变量.我知道Java wount支持它.但实现同样目标的更好方法是什么?我不希望我的类中的其他方法访问变量,但它应该在方法的调用中保留值.
有人可以让我知道.
我使用了一个静态变量,我的静态变量默认值是1.那么这个变量值就会根据用户登录而改变.我的静态变量值用于根据用户登录更改主题.
public static int theme = 1;
Run Code Online (Sandbox Code Playgroud)
我已经检查了5个用户登录,同时它工作正常,但我想知道数百万用户在我的网站同时登录这个静态变量是否失败?
请给我一些建议
问候,贾丁
bool _hintExist;
- (void)showNotReachable
{
if (_hintExist) {
return;
}
NSLog(@"Show a hint");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
_hintExist = NO;
});
}
Run Code Online (Sandbox Code Playgroud)
上面的代码很好.有一个按钮可以触发该方法.实际上,我不需要_hintExist是一个全局变量.我想在方法中制作bool _hintExist.但是,当我尝试在bool _hintExist前添加static和__block时.会有编译错误.令我惊讶的是,如果我把它变成一个全局变量,我可以在块中更改_hintExist.有人能解释为什么吗?如果我在bool _notReachableHintExist之前添加静态,我的代码中的全局变量有什么区别?
请参阅以下代码:
#include <iostream>
using namespace std;
struct T
{
~T()
{
cout << "deconstructor calling\n";
}
};
static T& get1()
{
static T x;
return x;
}
static T& get2()
{
static T& x = *new T;
return x;
}
int main()
{
get1();//calls the deconstructor
get2(); //dosent call the deconstructor
}
Run Code Online (Sandbox Code Playgroud)
为什么get1叫解构主义但get2不是?据我所知,当你终止程序时,静态变量会被破坏!但为什么在调用get1程序后调用静态变量的解构?
我有一个类似帖子的阅读:
有人说:"函数静态变量的生命周期首次开始[0]程序流遇到声明,它在程序终止时结束."
这似乎不是真的!
Static1.hpp
#include <string>
class Static1
{
public:
static const std::string my_string;
};
Run Code Online (Sandbox Code Playgroud)
Static1.cpp
#include "Static1.hpp"
const std::string Static1::my_string = "aaa";
Run Code Online (Sandbox Code Playgroud)
Static2.hpp
#include <string>
class Static2
{
public:
static const std::string my_string;
};
Run Code Online (Sandbox Code Playgroud)
Static2.cpp
#include "Static2.hpp"
const std::string Static2::my_string = Static1::my_string;
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include "Static2.hpp"
#include <iostream>
int main(argc int, char** argv)
{
cout << to_string(Static2::my_string == "aaa") << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我放入add_executable(printMyString main.cpp Static2.cpp Static1.cpp)我的CMakeLists.txt,我会得到
0
Run Code Online (Sandbox Code Playgroud)
虽然add_executable(printMyString main.cpp Static2.cpp Static1.cpp)给了我预期的行为
1
Run Code Online (Sandbox Code Playgroud)
为了使我的代码更容易维护(这样我就不需要跟踪我列出源文件的顺序),有什么办法可以确保我得到的行为在哪里Static2::my_string == "aaa"?
我有以下程序:
#include <iostream>
void Init();
struct Foo {
Foo() {
int *p = new int; // just to make sure Foo's ctor is not a constant expression
Init();
}
} foo;
struct Bar {
constexpr Bar()
: value(0) { }
int value;
} bar;
void Init() {
bar.value = 1;
}
int main()
{
std::cout << bar.value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这里foo的构造函数不是常量表达式,因此我们将动态初始化foo。但是bar的构造函数似乎是一个常量表达式,因此我们将对进行静态初始化bar。因此,bar必须先调用的ctor,然后foo将其1视为输出。我在GCC 8.3.0和Clang 8.0.0中观察到了这样的结果。但是对于Visual C ++,实际的输出是,0 …
c++ static-variables visual-c++ static-initialization constexpr
static-variables ×10
c++ ×4
java ×3
.net ×1
c# ×1
class ×1
constexpr ×1
gcc ×1
java-threads ×1
lifetime ×1
objective-c ×1
static ×1
thread-sleep ×1
visual-c++ ×1