小编Man*_*Row的帖子

静态pthreads互斥初始化

使用pthreads,如何在C中初始化静态的互斥量数组?

对于单个静态互斥锁,似乎我可以使用PTHREAD_MUTEX_INITIALIZER.但是它们的静态数组呢?例如,在

#include <pthread.h>
#define NUM_THREADS 5

/*initialize static mutex array*/
static pthread_mutex_t mutexes[NUM_THREADS] = ...?

或者必须动态分配?

c mutex pthreads static-initialization

18
推荐指数
2
解决办法
4万
查看次数

使用cmpxchg的x86螺旋锁

我是新手使用gcc内联汇编,并且想知道在x86多核机器上是否可以实现自旋锁(没有竞争条件)(使用AT&T语法):

spin_lock:
mov 0 eax
lock cmpxchg 1 [lock_addr]
jnz spin_lock
ret

spin_unlock:
lock mov 0 [lock_addr]
ret

x86 assembly gcc synchronization spinlock

14
推荐指数
1
解决办法
1万
查看次数

在pthreads中实现FIFO互斥

我正在尝试实现支持并发插入的二叉树(甚至可能在节点之间发生),但无需为每个节点分配全局锁或单独的互斥锁或互斥锁.相反,分配的此类锁的数量应该是使用树的线程数量级.

因此,我最终遇到了一种锁定护航问题.更简单地解释一下,当两个或多个线程执行以下操作时,可能会发生这种情况:

1 for(;;) {
2   lock(mutex)
3   do_stuff
4   unlock(mutex)
5 }

也就是说,如果线程#1在一个"cpu突发"中执行指令4-> 5-> 1-> 2,那么线程#2将从执行中匮乏.

另一方面,如果pthreads中存在用于互斥锁的FIFO类型锁定选项,则可以避免这样的问题.那么,有没有办法在pthreads中实现FIFO类型的互斥锁?可以改变线程优先级吗?

c mutex pthreads

12
推荐指数
2
解决办法
2万
查看次数

Spring - @Primary对@ComponentScan失败了吗?

对于一个简单的POJO:

@Component
public class Foo
{
    private final String string;

    public Foo()
    {
        this("Secondary ComponentScan??");
    }

    public Foo(String string)
    {
        this.string = string;
    }

    @Override
    public String toString()
    {
        return string;
    }
}
Run Code Online (Sandbox Code Playgroud)

和这个配置

@Configuration
@ComponentScan(basePackageClasses = Foo.class)
public class TestConfiguration
{
    @Primary
    @Bean
    public Foo foo()
    {
        return new Foo("Primary bean!!");
    }
}
Run Code Online (Sandbox Code Playgroud)

我期待以下测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfiguration.class)
public class Test
{
    @Autowired
    private Foo foo;

    @Test
    public void test()
    {
        System.out.println(foo);
    }
}
Run Code Online (Sandbox Code Playgroud)

打印出来,Primary Bean!!但它返回Secondary …

java testing configuration spring spring-test

11
推荐指数
1
解决办法
3921
查看次数

类定义中的引用变量

我正在学习C++,并且我读到所有引用必须在声明时初始化,并且不能有"未初始化的引用".但是如果引用变量是类成员呢?

class test
{
    int &k;
};

int main()
{
    test *abc = new test;
}
Run Code Online (Sandbox Code Playgroud)

该程序编译并正常运行(以g ++为单位,没有警告).但是,它abc->k是一个参考,但它初始化为什么?或者,它是某种"未初始化的参考",还是其他什么?

c++ reference

10
推荐指数
1
解决办法
1万
查看次数

Mockito吞下了堆栈痕迹?

为什么Mockito吞下堆栈痕迹?例如,如果我有

public class Foo
{
    public void foo()
    {
        bar();
    }

    public void bar()
    {
        baz();
    }

    public void baz()
    {
        throw new RuntimeException();
    }
}
Run Code Online (Sandbox Code Playgroud)

和一个测试,如

public class MockTest
{
    @Test
    public void test()
    {
        Mockito.spy(new Foo()).foo();
    }
}
Run Code Online (Sandbox Code Playgroud)

抛出的异常总是看起来像

java.lang.RuntimeException
    at Foo.baz(Foo.java:17)
    at MockTest.test(MockTest.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at …

java junit unit-testing mocking mockito

9
推荐指数
2
解决办法
1231
查看次数

自动装配Spring超类

为什么Spring在自动装配期间自动选择超类类型?

例如,如果我有

@Component
public class Foo {}

@Component
public class Bar extends Foo {}
Run Code Online (Sandbox Code Playgroud)

和某人自动装配

@Autowired
private Foo foo;
Run Code Online (Sandbox Code Playgroud)

为什么Spring总是选择超类型Foo?这不应该是一个" 模糊 "的映射(并导致Spring抛出错误)?

你在技术上不是有两个 Foo候选人吗?(例如,当从Foo中移除@Component时,会自动选择 Bar ...)

java inheritance spring javabeans autowired

6
推荐指数
1
解决办法
3459
查看次数

发信号通知条件变量(pthreads)

假设某些条件变量"cond"与互斥变量"mutex"相关联.如果线程cond在调用后处于休眠状态pthread_cond_wait(&cond,&mutex),并且另一个已mutex锁定的线程已完成,则该线程在调用pthread_cond_signal(&cond)之前或之后调用pthread_mutex_unlock(&mutex)是否重要?它是否甚至需要解锁互斥锁pthread_cond_signal(&cond),因为睡眠线程无论如何都会获取互斥锁?

编辑:根据https://computing.llnl.gov/tutorials/pthreads/#ConVarOverview,"调用pthread_cond_signal()后未能解锁互斥锁可能不允许匹配的pthread_cond_wait()例程完成(它将保持阻塞状态). " 我想那时,解锁,也许只是之后才需要.

c posix mutex pthreads condition-variable

5
推荐指数
1
解决办法
3973
查看次数

Jackson EnumMap 键序列化

Jackson 是否允许您自定义它序列化 EnumMap 键的方式?例如,如果我有

public enum MyKey
{
    ABC, DEF, XYZ;

    public String getKey()
    {
        return "my-key-" + ordinal();
    }
}
Run Code Online (Sandbox Code Playgroud)

还有一些

public class MyObject
{
    private final Map<MyKey,String> map = new EnumMap<>(MyKey.class);

    public MyObject()
    {
        map.put(MyKey.ABC, "foo");
        map.put(MyKey.DEF, "bar");
        map.put(MyKey.XYZ, "baz");
    }

    public Map<MyKey,String> getMap()
    {
        return map;
    }
}
Run Code Online (Sandbox Code Playgroud)

那么杰克逊将序列MyObject化为

{"map":{"ABC":"foo","DEF":"bar","XYZ":"baz"}}

相反,我希望它像这样序列化

{"map":{"my-key-0":"foo","my-key-1":"bar","my-key-2":"baz"}}。我不想toString()为了这个工作而覆盖任何内容。这在杰克逊身上有可能吗?

我尝试这样做:

public class MyKeySerializer extends JsonSerializer<MyKey>
{
    @Override
    public void serialize(MyKey value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException …
Run Code Online (Sandbox Code Playgroud)

java serialization json jsonserializer jackson

4
推荐指数
1
解决办法
3575
查看次数

JPA中的空版本字段?

http://docs.oracle.com/javaee/6/api/javax/persistence/Version.html中没有任何内容表明它不能为空.

跑完之后

import javax.validation.Validation;
import javax.validation.Validator;

import org.junit.Test;

import static org.junit.Assert.assertFalse;

public class Test
{
    @Test
    public void test()
    {
        //Using Hibernate Validator 4.1.0.Final...
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        assertFalse(validator.validate(new Foo()).isEmpty()); //why does this fail??
    }
}
Run Code Online (Sandbox Code Playgroud)

其中Foo.class很简单

import javax.persistence.Entity;
import javax.persistence.Version;

@Entity 
public class Foo
{
    @Version
    private final Integer version = null;
}
Run Code Online (Sandbox Code Playgroud)

测试失败,并且未报告@Version验证错误.

这是否意味着@ Version-annotated字段可以为null?

java hibernate jpa version bean-validation

3
推荐指数
1
解决办法
2313
查看次数

Spring代理请求属性吗?

如果某个地方我有一个单身@Component豆"富"了@Autowired的HttpSession或HttpServletRequest的,不富本身必须声明会话(或请求)范围的,或者我可以把它作为一个简单的单---在这种情况下HttpSession中和/或HttpServletRequest可能已经通过Spring注入了作为范围代理吗?

java session spring servlets spring-mvc

3
推荐指数
1
解决办法
1201
查看次数

多次绑定到同一个端口?

为什么下面的代码没有抛出"java.net.BindException:Address in in use:JVM_Bind"异常?

import java.net.InetSocketAddress;
import java.net.ServerSocket;

public class Test
{
    public static void main(String[] args) throws Exception
    {
        try (ServerSocket socket1 = new ServerSocket();
             ServerSocket socket2 = new ServerSocket();
             ServerSocket socket3 = new ServerSocket())
        {
            int port = 10000;

            socket1.setReuseAddress(false);
            socket1.bind(new InetSocketAddress("0.0.0.0", port));

            socket2.setReuseAddress(false);
            socket2.bind(new InetSocketAddress("127.0.0.1", port));

            socket3.setReuseAddress(false);
            socket3.bind(new InetSocketAddress("127.0.0.2", port));

            Thread.sleep(Long.MAX_VALUE);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

之后运行'netstat'显示:

C:\Users\Administrator>netstat -a -n | findstr 10000
  TCP    0.0.0.0:10000          0.0.0.0:0              LISTENING
  TCP    127.0.0.1:10000        0.0.0.0:0              LISTENING
  TCP    127.0.0.2:10000        0.0.0.0:0              LISTENING
  TCP    [::]:10000             [::]:0 …

java sockets windows networking windows-networking

2
推荐指数
1
解决办法
3136
查看次数