这是永久的“如果或转换”困境的变体......
考虑一个使用静态方法的多线程应用程序,该方法包含一个长(十几个条件)if语句,该语句检查对象的类型并相应地返回一个值,即类似
public static String checkType(Class<?> type)
{
if (type == A.class)
{
return aString;
}
else if (type == B.class)
{
return bString;
}
...
else if (type == z.class)
{
return zString;
}
}
Run Code Online (Sandbox Code Playgroud)
显然 switch 语句在这里不能直接适用,所以一个常见的模式是有一个enum并调用它的valueOf(),即做类似的事情
public enum Strings
{
A(aString), B(bString), ..., Z(zString)
private final String value;
private Strings(String value)
{
this.value = value;
}
public String value()
{
return this.value;
}
}
Run Code Online (Sandbox Code Playgroud)
所以,checkType()可以改写为
public static …Run Code Online (Sandbox Code Playgroud) 你好,我试图在我的塔防中添加线程以使其更快,但现在速度变慢了。
代码结构非常简单
主要以 sdl opengl init 和 init 一切开始。然后游戏循环。无线程顺序: 1:keyboard and mouse event first 2:gameManager 3:drawGlScene
游戏管理器计算一切:移动怪物,攻击怪物,创建攻击动画和声音,检查你是赢了还是输了,如果波完成了,怪物产生,如果速度模式打开,这个功能会运行2次。和其他一些小功能。
绘图功能使用所有数据来绘制所有内容。使用绘图功能有 0 个数据修改
我使用的 cpu 是四核,这里是主要的可视化部分第一步初始化线程的东西
int main ( int argc, char** argv )
{
pthread_t t_engine;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
Run Code Online (Sandbox Code Playgroud)
然后所有其他初始化内容和游戏循环开始以 sdl 事件切换然后(仍在游戏循环中):
//calculate everything if we are in playing gamestate
if(id == MODE_PLAY)
{
rc = pthread_create(&t_engine, &attr, gameManager, (void *)t);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
//gameManager((void *)t);
}
//draw everything
DrawGLScene(); …Run Code Online (Sandbox Code Playgroud) 我有多个线程从一个标记为 final 的共享数组访问元素(我永远不会尝试为它分配另一个数组)。我需要同步任何东西吗?我可以假设对元素的读/写是原子的吗?
我想不断检查数据库中的表以获取要运行的命令。某些命令可能需要 4 分钟才能完成,大约需要 10 秒。
因此我想在线程中运行它们。所以每条记录都会创建新的线程,并且在创建线程后,记录被删除。
因为数据库查找 + 线程创建将在无限循环中运行,我如何从线程获取“响应”(线程将发出 shell 命令并获取我想阅读的响应代码)?
我想过创建两个无限循环的线程: - 第一个用于数据库查找 + 创建新线程 - 第二个用于......以某种方式读取线程结果并对每个响应采取行动
或者也许我应该使用 fork,或者 os 产生一个新进程?
我有单身课程
class DeviceController:NSObject, CocoaMQTTDelegate {
static let sharedInstance = DeviceController()
var deviceOnArray:[String] = []
var deviceOffArray:[String] = []
private override init() {
clientID = "xyz-" + String(ProcessInfo().processIdentifier)
mqtt = CocoaMQTT(clientID: clientID, host: "device_controller.xyz.net", port: 1883)
mqtt.username = "username"
mqtt.password = "password"
mqtt.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
mqtt.keepAlive = 30
mqtt.cleanSession = true
DeviceController.isConnecting = true
super.init()
mqtt.delegate = self
mqtt.connect()
self.registerBackgroundTask()
}
func sendArm(topic:String){
// add device to deviceOnArray
}
func sendDisarm(topic:String){
// remove device from deviceOnArray if exist …Run Code Online (Sandbox Code Playgroud) std :: mutex是多线程的成员变量线程安全吗?
有一个类的实例有一个变量和互斥锁作为成员.
每个函数都在不同的线程中调用.
我很好奇,可以使用这样的互斥量吗?
有许多示例代码使用一种包装类来保护互联网,如卫士或其他东西.
我更喜欢简单地使用它,并希望明确解锁.不是在破坏时间.
#include <mutex>
#include <stdio.h>
class A {
private:
bool _is;
std::mutex _mutex;
}
// this function runs in Thread A
void A::read() {
_mutex.lock();
printf("%d", _is);
_mutex.unlock();
}
// this function runs in Thread B
void A::write() {
_mutex.lock();
printf("%d", _is);
_is = !_is;
_mutex.unlock();
}
Run Code Online (Sandbox Code Playgroud) 我最近读了一本书"Java Concurrency in Practice 2nd",作者提到如果我们使用Collections.synchronizedList来创建一个安全的线程List,那么我们必须确保我们使用的是来自SynchronizedCollection的同一个锁.下面的代码来自这本书:
public class ListHelper <E> {
public List<E> list = Collections.synchronizedList(new ArrayList<E>());
public synchronized boolean putIfAbsent(E x) {
boolean absent = !list.contains(x);
if (absent)
list.add(x);
return absent;
}
}
Run Code Online (Sandbox Code Playgroud)
在这个类中,方法putIfAbsent已被ListHelper中的Object锁定,但是list.contains不使用此Object作为锁,有两个锁,因此在多线程下它不安全.但我的问题是如何证明它不是线程安全的.你有什么想法?
如果我想从另一个线程更新主线程中的主UI控件的值,请使用以下代码
第一次尝试 - 它有效
Label1.Invoke((MethodInvoker)(()=> Label1.Text = "label"));
button1.Invoke((MethodInvoker)(()=> button1.Text = "button"));
textBox1.Invoke((MethodInvoker)(()=> textBox1.Text = "textBox"));
Run Code Online (Sandbox Code Playgroud)
第二次尝试 - 它有效
this.Invoke((MethodInvoker)(()=>
{
Label1.Text = "label";
button1.Text = "button";
textBox1.Text = "textBox";
}));
Run Code Online (Sandbox Code Playgroud)
第二次尝试看起来很简单,因为只需要编写"this.Invoke"
第二次尝试访问主线程UI控件简单
使用2nd尝试"this.Invoke"有什么问题吗?
我想知道第二次尝试是否安全第二次尝试
是否有可能导致任何问题?
我们可以containsKey()在没有在多线程环境中进行同步的情况下使用Hashmap的方法吗?
注意:线程只会读取Hashmap。该映射只初始化一次,以后再也不会修改。
我不知道为什么我的代码不是线程安全的,因为它输出了一些不一致的结果。
value 48
value 49
value 50
value 54
value 51
value 52
value 53
Run Code Online (Sandbox Code Playgroud)
我对原子对象的理解是,它防止其中间状态暴露出来,因此当一个线程正在读取它而另一线程正在写入它时,它应该解决该问题。
我曾经认为我可以使用没有互斥量的std :: atomic来解决多线程计数器增量问题,但情况并非如此。
我可能误解了原子对象是什么,有人可以解释吗?
value 48
value 49
value 50
value 54
value 51
value 52
value 53
Run Code Online (Sandbox Code Playgroud)