MSDN 在C#中给出了关于lock关键字的以下警告:
通常,避免锁定公共类型或超出代码控制范围的实例.常见的构造锁(this),lock(typeof(MyType))和lock("myLock")违反了这个指南:
Run Code Online (Sandbox Code Playgroud)* lock (this) is a problem if the instance can be accessed publicly. * lock (typeof (MyType)) is a problem if MyType is publicly accessible.
然而,它没有为它提供任何可靠的推理.锁(这个)在这里解释了SO.我对lock(typeof(MyType))案件感兴趣.有什么危险吗?
谢谢.
我花了一整天的时间试图让我的应用程序使用线程,但没有运气.我已经阅读了很多关于它的文档,我仍然会遇到很多错误,所以我希望你能帮助我.
我有一个耗时的方法,它调用数据库并更新GUI.这必须一直发生(或大约每30秒).
public class UpdateController
{
private UserController _userController;
public UpdateController(LoginController loginController, UserController userController)
{
_userController = userController;
loginController.LoginEvent += Update;
}
public void Update()
{
BackgroundWorker backgroundWorker = new BackgroundWorker();
while(true)
{
backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
backgroundWorker.RunWorkerAsync();
}
}
public void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
_userController.UpdateUsersOnMap();
}
}
Run Code Online (Sandbox Code Playgroud)
使用这种方法我得到一个例外,因为后台工作者不是和STA线程(但从我可以理解这是我应该使用的).我试过一个STA线程,并给出了其他错误.
我认为问题是因为我在进行数据库调用时尝试更新GUI(在后台线程中).我应该只进行数据库调用,然后以某种方式它应该切换回主线程.在主线程执行后,它应该返回后台线程,依此类推.但我看不出怎么做.
应用程序应在数据库调用后立即更新GUI.Firering事件似乎不起作用.背景线程只是输入它们.
编辑:
一些非常好的答案:)这是新的代码:
public class UpdateController{
private UserController _userController;
private BackgroundWorker _backgroundWorker;
public UpdateController(LoginController loginController, UserController userController)
{
_userController = userController;
loginController.LoginEvent += Update;
_backgroundWorker = new BackgroundWorker(); …Run Code Online (Sandbox Code Playgroud) 假设我们有这个简单的例子:
public Example extends Thread{
String temp;
public Example(){
}
@Override
public void run(){
.
.
.
.
temp = "a_value";
}
public static void main(String[] args) {
Example th = new Example();
th.start();
}
}
Run Code Online (Sandbox Code Playgroud)
Thread完成工作后怎么能给我返回String temp?
注意:它的错误是在WaitHandle.WaitAll(doneEvents)行; 我正在使用标准的WPF项目.
private void Search()
{
const int CPUs = 2;
var doneEvents = new ManualResetEvent[CPUs];
// Configure and launch threads using ThreadPool:
for (int i = 0; i < CPUs; i++)
{
doneEvents[i] = new ManualResetEvent(false);
var f = new Indexer(Paths[i], doneEvents[i]);
ThreadPool.QueueUserWorkItem(f.WaitCallBack, i);
}
// Wait for all threads in pool
WaitHandle.WaitAll(doneEvents);
Debug.WriteLine("Search completed!");
}
Run Code Online (Sandbox Code Playgroud)
更新:以下解决方案不适用于WPF应用程序!无法将主应用程序属性更改为MTAThreadAttribute.这将导致以下错误:
错误: "不支持在STA线程上使用多个句柄的WaitAll."
我对如何使用pthread声明递归互斥锁感到困惑.我尝试做的是一次只有一个线程能够运行一段代码(包括函数)但是在怀疑之后我发现使用互斥量不起作用而我应该使用递归互斥锁.这是我的代码:
pthread_mutex_lock(&mutex); // LOCK
item = queue_peek(queue); // get last item in queue
item_buff=item; // save item to a buffer
queue_removelast(queue); // remove last item from queue
pthread_mutex_unlock(&mutex); // UNLOCK
Run Code Online (Sandbox Code Playgroud)
所以我尝试做的只是串行读取/删除队列.
问题在于,没有任何关于如何声明递归互斥体的例子.或者可能有一些,但他们不为我编译.
我正在尝试通过以下方式传递参数:
Thread thread = new Thread(new ParameterizedThreadStart(DoMethod));
Run Code Online (Sandbox Code Playgroud)
知道怎么做吗?我很感激一些帮助
如何在不是线程的对象上调用wait()和notify()方法?那真的没有意义,是吗?
当然,它必须有意义,因为这两种方法可用于所有Java对象.有人可以提供解释吗?我无法理解如何使用wait()和之间的线程之间进行通信notify().
我不知道std::atomic变量,但是知道std::mutex标准提供的(奇怪的权利!); 然而有一件事引起了我的注意:标准提供了两种看似相同(对我而言)的原子类型,如下所示:
它也以 - 的例子提到std::atomic_flag type-
std :: atomic_flag是一种原子布尔类型.与std :: atomic的所有特化不同,它保证是无锁的.与std :: atomic不同,std :: atomic_flag不提供加载或存储操作.
我不明白.std::atomic bool type不保证是无锁的吗?那它不是原子的还是什么?
那么两者之间有什么区别呢?我应该在何时使用哪种?
如何以编程方式证明StringBuilder不是线程安全的?
我尝试过这个,但它不起作用:
public class Threadsafe {
public static void main(String[] args) throws InterruptedException {
long startdate = System.currentTimeMillis();
MyThread1 mt1 = new MyThread1();
Thread t = new Thread(mt1);
MyThread2 mt2 = new MyThread2();
Thread t0 = new Thread(mt2);
t.start();
t0.start();
t.join();
t0.join();
long enddate = System.currentTimeMillis();
long time = enddate - startdate;
System.out.println(time);
}
String str = "aamir";
StringBuilder sb = new StringBuilder(str);
public void updateme() {
sb.deleteCharAt(2);
System.out.println(sb.toString());
}
public void displayme() {
sb.append("b");
System.out.println(sb.toString());
}
} …Run Code Online (Sandbox Code Playgroud)