相关疑难解决方法(0)

atomic/volatile/synchronized有什么区别?

原子/易失性/同步如何在内部工作?

以下代码块之间有什么区别?

代码1

private int counter;

public int getNextUniqueIndex() {
    return counter++; 
}
Run Code Online (Sandbox Code Playgroud)

代码2

private AtomicInteger counter;

public int getNextUniqueIndex() {
    return counter.getAndIncrement();
}
Run Code Online (Sandbox Code Playgroud)

代码3

private volatile int counter;

public int getNextUniqueIndex() {
    return counter++; 
}
Run Code Online (Sandbox Code Playgroud)

是否volatile以下列方式工作?是

volatile int i = 0;
void incIBy5() {
    i += 5;
}
Run Code Online (Sandbox Code Playgroud)

相当于

Integer i = 5;
void incIBy5() {
    int temp;
    synchronized(i) { temp = i }
    synchronized(i) { i = temp + 5 }
}
Run Code Online (Sandbox Code Playgroud)

我认为两个线程不能同时进入同步块...我是对的吗?如果这是真的那么如何atomic.incrementAndGet()工作没有synchronized …

java multithreading synchronization atomic volatile

282
推荐指数
4
解决办法
13万
查看次数

如何在没有任何用处的情况下停止永远运行的线程

在下面的代码中,我有一个while(true)循环.考虑到try块中存在一些代码的情况,其中线程应该执行一些约需一分钟的任务,但是由于某些预期的问题,它正在运行.我们可以阻止那个线程吗?


public class thread1 implements Runnable {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        thread1 t1 = new thread1();
        t1.run();

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(true){
            try{        
                Thread.sleep(10);

            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java multithreading

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

Tomcat 6内存泄漏日志条目

以下是我在CentOS机器上的Catalina.out文件中输入的唯一条目.我正在使用Spring 3和我的应用程序运行Tomcat 6.有很多这样的,所以我选择了一些不断重复的东西.这不会一直发生,但每周至少发生一次.

问题是我该如何防止波纹发生?

Feb 3, 2011 2:37:48 PM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc

SEVERE: The web application [] registered the JBDC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

Feb 3, 2011 2:37:48 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads

SEVERE: The web application [] appears to have started a thread named [com.iteezy.shared.domain.DirEntry.data] but has failed to stop it. This is very likely to create a memory leak.


Feb …
Run Code Online (Sandbox Code Playgroud)

java spring memory-leaks tomcat6 catalina.out

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