如何在一个线程中安全地处理一个对象,而另一个线程可以在Delphi中使用该对象?有什么想法或文章要阅读?
我有这样的地图
map<string,A>
Run Code Online (Sandbox Code Playgroud)
现在,当我从线程I迭代地图时,线程II正在向它插入一些内容.这种插入是否会影响阅读?
在关于线程的Java课程的最后几分钟,我们的教授建议我们在开发复杂的基于Swing的应用程序时要特别注意,因为Swing
它不是线程安全的.这背后有什么具体原因吗?Swing
由于设计决策或软件限制,是不是线程安全的?在此先感谢您的回答.
我ConcurrentLinkedQueue
在一组线程处理它们之前使用a 来排队元素:
public class AsyncActionQueue implements Serializable {
...
private Queue<QueuedAction> queue;
public AsyncActionQueue() {
this.queue = new ConcurrentLinkedQueue<>();
}
...
private class QueueProcessor implements Runnable {
private boolean isKilled = false;
public void run() {
while(!isKilled) {
QueuedAction action = queue.remove();
Run Code Online (Sandbox Code Playgroud)
queue.remove()
如果队列中没有更多元素,我认为会返回null,而是我得到一个NoSuchElementException
.
这是一种情况,其中队列是单例,并且可以使用弹性数量的线程来清空它.在尝试删除之前,线程检查队列中是否有元素的最佳方法是什么?我应该检查是否queue.size() > 0
,这意味着线程安全吗?
我必须在文件中写入使用线程的彩票产生的所有可能组合.例:
在我的主类我只使用一个线程,因为我不知道如何更快地使用更多线程写入文件.
为了生成数字,我使用6个循环,一个在另一个内部,这样:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class Proceso extends Thread {
File file = new File("lottery.txt");
public Proceso(String msg) {
super(msg);
}
public void run() {
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
StringBuffer linea = new StringBuffer();
for (int …
Run Code Online (Sandbox Code Playgroud) 我想知道,如果尝试更新线程使用的布尔值,则保证成功,没有任何锁定保护.
类似下面的情况:当threadproc正在运行时,Stop()更改m_ThreadActive的布尔成员不会有任何问题吗?
private bool m_ThreadActive = true;
public void threadproc
{
while (m_ThreadActive)
{
...
}
}
public void Stop()
{
m_ThreadActive = false;
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有以下按钮的GUI.当它被按下时,它应该运行一个新线程(我有一个实现Runnable的类).但是,当我这样做时,GUI会冻结.我做错了什么,如何解决?
//button
addKitchenStaff.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
//create object of a class which implements runnable
KitchenStaff kitchenStaff = new KitchenStaff(allDishes, ingredientsModel, dishesModel,communication.getBap());
//arraylist of all such objects
allKitchenStaff.add(kitchenStaff);
Thread thread = new Thread(kitchenStaff);
thread.run();
}
});
Run Code Online (Sandbox Code Playgroud) 在Java中我们可以做类似的事情
final string str = "I am a string";
Thread thread = new Thread(...{
string str2 = "bla bla bla " + str;
});
Run Code Online (Sandbox Code Playgroud)
我不完全理解如何做到这一点,但重点是我需要声明str为最终在我的线程中访问它所以没有可变的腐败.
但现在我如何在C#中做到这一点
string str = "I am a string";
Task.Run(() => {
string str2 = "bla bla " + str;
});
Run Code Online (Sandbox Code Playgroud)
它确实有效,但我不确定这是否正确.我正在使用Visual Studio for Xamarin.
我想建立一个unordered_map,同时从一个写入器和多个读取线程进行访问。编写器将仅插入一对,而不删除或更新插入的任何内容。这个线程安全吗?我可以将插入操作视为原子操作吗?我对代码进行了多次测试,未发现崩溃或错误结果。
谢谢!
我的代码试图传递一个std :: map作为线程的引用,但似乎有些不好并导致
error: invalid conversion from ‘void* (*)(std::map<std::basic_string<char>,
std::vector<std::basic_string<char> > >)’ to ‘void* (*)(void*)’ [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
我需要将map传递给线程并在该线程中插入map的键和值,并在成功之后.在主进程中,我需要更新或复制(线程映射)在同一映射的另一个对象,即myMapcache
int main()
{
std::map< std::pair<std::string , std::string> , std::vector<std::string> > myMap,myMapCache;
pthread_t threads;
//how to pass map object in thread as reference
int rc = pthread_create(&threads, NULL, myfunction, std::ref(myMap));
if (rc)
{
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
// if true then update to local myMapCache
if(update)
{
std::copy(myMap.begin(), myMap.end(), std::inserter(MyMapCache, myMapCache.end()) );
}
}
void * …
Run Code Online (Sandbox Code Playgroud) thread-safety ×10
java ×4
c++ ×3
c# ×2
swing ×2
.net ×1
combinations ×1
concurrency ×1
delphi ×1
dictionary ×1
jbutton ×1
performance ×1
pthreads ×1
queue ×1
theory ×1
xamarin ×1