我试图在java中的另一个线程中访问和修改线程的变量,我真的不知道如何做到这一点.
例如:
Runnable r1 = new Runnable() {
int value = 10;
public void run() {
// random stuff
}
}
Runnable r2 = new Runnable() {
public void run() {
// of course the bellow line will not work
r1.value--; // I want here to be able to decrement the variable "value" of r1
}
}
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
Run Code Online (Sandbox Code Playgroud)
谢谢你,如果你有任何想法!
有没有办法在java中为线程创建一个getter和setter?
编辑:答案很好,但我不清楚我的问题,我会尝试提出一个更好的问题
我想在 cout 中截断一个字符串,
string word = "Very long word";
int i = 1;
cout << word << " " << i;
Run Code Online (Sandbox Code Playgroud)
我希望将最多 8 个字母作为字符串的输出
所以就我而言,我想要
Very lon 1
Run Code Online (Sandbox Code Playgroud)
代替 :
Very long word 1
Run Code Online (Sandbox Code Playgroud)
我不想使用 wget(8) 函数,因为不幸的是它不会将我的单词截断为我想要的大小。我也不希望“单词”字符串更改其值(我只想向用户显示单词的一部分,但将其保留在我的变量中)
提前致谢
编辑:有人发布了解决方案,它对我有用,现在我无法将他的答案标记为好,因为他在被否决后删除了他的答案。它就像使用 word.substr(0, 8) 方法一样简单。他有什么理由被否决吗?这种方法不好用吗?
我想使用 Visual C++ 2010 专业版编译 64 位应用程序,但我不断收到此错误,我不知道该怎么做:
1>------ Build started: Project: Test, Configuration: Debug x64 ------
1>Error: The "ConfigurationGeneral" rule is missing from the project.
Run Code Online (Sandbox Code Playgroud)
我在谷歌上搜索过这个问题,但所有的想法都没有解决我的问题。
谢谢!
如果需要此信息,我有 Windows 8.1 Pro 64 位,并且我使用 Visual Studio 2010 c++ 专业版。
编辑:尝试修复 Visual Studio Professional,但它仍然不起作用:\
我在窗口中有一个画布。窗口是全屏的,但画布不是。我需要在画布内获取鼠标位置,但不在窗口内。我该怎么做呢?
即使鼠标没有移动,我也需要每秒多次获得该鼠标位置,因此使用 MouseMoved 侦听器还不够好。
有没有办法在画布中获取鼠标位置而不必调用任何鼠标侦听器?