Java中的多个线程如何处理传递给它们的单个对象引用?
他们是复制该对象然后使用它,还是使用同一个对象?
欢迎任何技术解释。
我无法在上下文中理解这一点:如果线程将一些数据传递给对象并进入睡眠状态,而一个线程正在睡眠时,另一个线程使用同一对象将数据传递给它并进入睡眠状态。
最后一个数据会覆盖对象中的前一个数据吗?
java multithreading thread-safety thread-synchronization java-threads
我正在尝试支持定期清除的哈希图上的并发性。我有一个缓存,可以存储一段时间的数据。每 5 分钟后,此缓存中的数据将发送到服务器。一旦我刷新,我想清除缓存。问题是当我刷新时,当我使用现有密钥执行此操作时,数据可能会写入此映射。我将如何使这个进程线程安全?
data class A(val a: AtomicLong, val b: AtomicLong) {
fun changeA() {
a.incrementAndGet()
}
}
class Flusher {
private val cache: Map<String, A> = ConcurrentHashMap()
private val lock = Any()
fun retrieveA(key: String){
synchronized(lock) {
return cache.getOrPut(key) { A(key, 1) }
}
}
fun flush() {
synchronized(lock) {
// send data to network request
cache.clear()
}
}
}
// Existence of multiple classes like CacheChanger
class CacheChanger{
fun incrementData(){
flusher.retrieveA("x").changeA()
}
}
Run Code Online (Sandbox Code Playgroud)
我担心上面的缓存没有正确同步。有没有更好/正确的方法来锁定这个缓存,这样我就不会丢失数据?我应该创建缓存的深层副本并清除它吗?
既然上面的数据可能被另一个更改器更改,那会不会导致问题?
java concurrency java.util.concurrent thread-synchronization kotlin
这是我的问题的简化版本.
在无限循环中有3个线程执行N个线程:
A -> B -> C -> A -> B -> C -> A -> B -> .......
Run Code Online (Sandbox Code Playgroud)
我希望所有线程同时执行指令B,即任何线程执行B只应在所有线程都已到达B时启动.因此,如果有一个线程执行了B - > C - > A,它应该在这里等待其他线程也准备好执行B.
如果可能的话,请告诉我一个可在Windows和MAC上运行的便携式解决方案.
我有一个用.NET 4.0编写的Windows窗体应用程序.最近,在执行一些测试时,我注意到句柄存在一些问题.下表显示了结果:

正如你所看到的,只有增加的句柄类型是Event.
所以我的问题是:所描述的问题是否可能是由Windows窗体应用程序引起的?我的意思是,我不使用AutoResetEvent或同步线程ManualResetEvent.我确实使用线程,但从上面的表中可以看出线程句柄的数量似乎没问题.那么,我认为他们是由CLR妥善管理的?
它可能是由我在我的应用程序中使用的任何第三方组件引起的吗?
如果不清楚,我会尽力回答你的问题.感谢帮助!
c# multithreading memory-leaks handles thread-synchronization
人们讲述了两种类型的多线程锁定 - 对象和类.据我所知,锁定仅在对象上完成.
案例1:我们使用的工具new或工厂方法等.
void synchronized myMethod(Type param) {
//will lock on the instance used to call this method
}
Run Code Online (Sandbox Code Playgroud)
要么
synchronized(this) {
//will lock on current object
}
Run Code Online (Sandbox Code Playgroud)
要么
synchronized(obj1) {
//will lock on specified obj1 object
}
Run Code Online (Sandbox Code Playgroud)
案例2:java.lang.Class对象
这称为类锁,可以与静态字段或方法或块一起使用,因为它们属于类并在所有对象和其他类属性之间共享.
static void synchronized method() {
//will lock the Class object
}
Run Code Online (Sandbox Code Playgroud)
要么
static {
synchronized(SomeClass.class){
int a = 2;
}
}
Run Code Online (Sandbox Code Playgroud)
java.lang.Class由JVM创建的对象中.因此抽象,它的对象锁定和图片中,我们看到类锁定.java.lang.Class实例)也以相同的方式工作.我想知道在同步静态方法的情况下,在以下两种情况下锁定线程获取哪个类:
到目前为止,这是我对这个问题的理解.请加上或纠正.
这是我在求职面试中提出的一个问题:
你有两个不同的类(实现Runnable)说EvenThread和OddThread.顾名思义,EvenThread只打印偶数,奇数线程只打印奇数,考虑范围为0-100.
class EvenThread implements Runnable {
@Override
public void run() {
for (int i = 0; i <= 10; i += 2) {
System.out.println(i);
}
}
}
class OddThread implements Runnable {
@Override
public void run() {
for (int i = 1; i < 10; i += 2) {
System.out.println(i);
}
}
}
public class EvenOdd {
public static void main(String args[]) {
Thread tEven = new Thread(new EvenThread());
Thread tOdd = new Thread(new OddThread());
tEven.start();
tOdd.start();
}
} …Run Code Online (Sandbox Code Playgroud) 这段代码是关于.
竞争条件:调度和编译器行为在进程或线程同步中发挥重要作用.演示同步需求的最简单方案来自两个线程/进程之间创建的竞争条件,试图修改共享变量的值,这通常会导致数据不一致和错误的结果.以下示例演示了这种情况:
我是C的新手,我遇到了这个警告发生的问题.警告意味着什么,我该如何解决它.我写的代码在这里:
q1.c: In function ‘runner’:
q1.c:13:1: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
printf("T tid: %d x before: %d\n", syscall(SYS_gettid),x); int i;
^
q1.c:19:1: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
printf("T tid: %d x after: %d\n", syscall(SYS_gettid),x);
Run Code Online (Sandbox Code Playgroud)
这是代码:
// Race condition
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
int x=0;
void * runner(void *arg) …Run Code Online (Sandbox Code Playgroud) 我正在寻找类似于这种语法的东西,即使它不存在.
我希望有一个方法作用于集合,并且在方法的生命周期中,确保集合不会被搞乱.
这可能看起来像:
private void synchronized(collectionX) doSomethingWithCollectionX() {
// do something with collection x here, method acquires and releases lock on
// collectionX automatically before and after the method is called
}
Run Code Online (Sandbox Code Playgroud)
但相反,我担心这样做的唯一方法是:
private void doSomethingWithTheCollectionX(List<?> collectionX) {
synchronized(collectionX) {
// do something with collection x here
}
}
Run Code Online (Sandbox Code Playgroud)
这是最好的方法吗?
在 PRAM 模型中,多个处理器同步操作以对不同的数据集执行相同的命令。
每种算法有两种读/写模式;
我觉得很难理解的是,这两种模式到底有什么区别,哪个似乎更精通?
algorithm computer-science processor synchronous thread-synchronization
我正在学习并发任务之间的合作,我已经得到了这个问题和可能的答案。我想确保我理解正确。
\n\n因此,要调用a.wait()它,首先需要在对象上同步a,或者更准确地说,线程必须成为a监视器的所有者。据我了解,a.wait()应该在同步上下文中调用(与a.notify()/一起a.notifyAll())的唯一原因是为了避免竞争条件/丢失唤醒问题。但理论上,可以通过在其他对象上同步来避免竞争条件调用,如下所示a.wait():a.notify()/a.notifyAll()
Thread #1: \nsynchronized(b) { \n \xe2\x80\xa6\n a.wait(); // here a thread owns only one monitor: the monitor of the object stored in the variable b, where a != b\n \xe2\x80\xa6\n}\n\nThread #2: \nsynchronized(b) {\n \xe2\x80\xa6\n a.notify(); // the same here\n \xe2\x80\xa6\n}\nRun Code Online (Sandbox Code Playgroud)\n\nThe expected behavior is: the thread #1 acquires the monitor of b, enters the critical section, invokes …
java ×6
concurrency ×2
algorithm ×1
c ×1
c# ×1
c++ ×1
handles ×1
java-threads ×1
kotlin ×1
locking ×1
memory-leaks ×1
processor ×1
synchronous ×1
unix ×1
wait ×1