小编Ben*_*Ben的帖子

如何使用AtomicBoolean阻止和等待

我正在寻找一种暂停线程的方法.

我开始有效地使用布尔标志(称为'暂停'),并使用while循环(暂停)包装检查.

在while循环中,有一个Thread.wait()阻止执行.

我一直在看AtomicBoolean,除了它不会阻止之外,它似乎可以解决这个问题.

是否有替代或扩展版本的AtomicBoolean有一个块方法?

即喜欢AtomicBoolean.getFalse()的东西AtomoicBoolean.get(false)

它们具有阻塞队列,因此具有阻塞值.

目前的设置是:

while (paused.get()) {
        synchronized (paused) {
            try {

                paused.wait();
            } catch (Exception e) {
            }

            paused.notify();
        }
    }
Run Code Online (Sandbox Code Playgroud)

public void pause() {
    if (paused.compareAndSet(false, true)) {
        synchronized (paused) {
            paused.notify();
        }
    }

}


public void resume() {
    if (paused.compareAndSet(true, false)) {
        synchronized (paused) {
            paused.notify();
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)

java concurrency atomic java.util.concurrent

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

GSON是Java Throwable

我有一个包含一段数据和相关异常的对象.

Public class MyBean{
  Private String data;
  Private Exception problem;
.
.
.
}
Run Code Online (Sandbox Code Playgroud)

当我尝试GSON.toJSON(对象)时,它给我一个循环引用错误抱怨"问题"字段.

有没有办法让GSON处理这些对象?

java exception gson

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

如何在测试期间在Spring中禁用@PostConstruct

在Spring组件中我有一个@PostConstruct声明.类似如下:

@Singleton
@Component("filelist")
public class FileListService extends BaseService {

    private List listOfFiles = new Arrays.list();

    //some other functions


    @PostConstruct
    public void populate () {

        for (File f : FileUtils.listFiles(new File(SystemUtils.JAVA_IO_TMPDIR), new String[]{"txt"},true)){
            listOfFiles.add(f.getName());
        }   
    }

    @Override
    public long count() throws DataSourceException {
        return listOfFiles.size();
    }

    //  more methods .....
}
Run Code Online (Sandbox Code Playgroud)

在单元测试期间,我不希望@PostConstruct调用该函数,有没有办法告诉Spring不要进行后期处理?或者是否有一个更好的注释用于在非测试中调用类的启动方法?

java spring annotations postconstruct

10
推荐指数
3
解决办法
7399
查看次数

为什么PriorityQueue不会像Queue一样?

我正在使用PriorityBlockingQueue优先级字段.在我的测试中,我使用System#currentTime()优先级 - 计算机获得相同的优先级,以至于毫秒是相同的(或者更像是PC上的毫秒具有误差幅度).

当优先级相同时,队列就像堆栈一样,这看起来很奇怪.当元素的优先级相同时,是否有一种替代方法可以使队列像正常队列一样(即FIFO而不是LIFO行为)?

java queue stack priority-queue

7
推荐指数
1
解决办法
1172
查看次数

错误:使用 Spring、Jersey 和 ehCache 时“同一 VM 中已存在另一个未命名的 CacheManager”

似乎有另一个进程正在加载缓存,但无法找到它(如果错误没有告诉我谎言)。它告诉我的唯一一件事是,我有另一个缓存从一个名为InputStreamConfigurationSource的类开始?

有人遇到过这个错误吗???

我在用

  • 春天3
  • 球衣1.6/1.7
  • ehCache 2.6

堆栈跟踪

SEVERE: Servlet.service() for servlet [SpringDispatcher] in context with path [/FileService] threw exception [com.sun.jersey.api.container.ContainerException: Unable to create resource class com.myapp.FileStoreAccessAction] with root cause
net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same  VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown …
Run Code Online (Sandbox Code Playgroud)

java spring ehcache jersey

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

@Async 通过代理类在 Spring 3.2 中创建异常

我似乎想到了一个问题。

我有课

@Component
@Scope("prototype")
public class MyClass extends BaseClass {

....
...
 @Async
 public void doSomething() {
 ....   
 }
....
} 
Run Code Online (Sandbox Code Playgroud)

和一个 Spring 配置,其中包含

<context:annotation-config />
<context:component-scan base-package="com.company.project" />
<task:annotation-driven executor="taskExecutor"/>
<task:executor id="taskExecutor" pool-size="10" queue-capacity="10" />
Run Code Online (Sandbox Code Playgroud)

在我的代码的某些部分

BaseClass bean = springBeans.getBean(MyClass.class);
Run Code Online (Sandbox Code Playgroud)

但我得到了这个例外

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'myClass' must be of type [com.company.project.MyClass], but was actually of type [$Proxy19]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:361)
Run Code Online (Sandbox Code Playgroud)

我可以理解它是一个代理类,但不知道为什么Spring不允许转换代理类。

我在类路径上有 cglib 2.2 no dep,以及 Spring 3.2 核心库。

任何人都可以指出解决此问题的任何线索吗?

简而言之,我希望一个方法在调用时是异步的。

java spring cglib spring-scheduled

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