ITeye博客

java并发小结

BBjava · 2011-10-16 14:34 · java并发 · 编程语言 · Markdown

1.原子性,原生数据类型的一般操作都是Atomic(原子性的),除了double,long这两个类型,因为在32位的CUP的操作系统中,处理64位的数据类型不可能原子性。 另外,自增、自减运算不是原子性的,如int a =0; a++;其中a++就是非原子性的。 问:为什么要原子性?原子性的操作不存在线程安全问题…… 2.可见性,可见性是指多个线程在竞争资源时,正在被某个线程使用的资源,其他线程能够看到(能够看到变化)。Volatile为关键字。(是否会引起同步问题?) 3.守护线程:又叫寄生线程,特殊的线程,主线程运行完了,守护线程生命周期结束;主线程没结束,守护线程自己运行完了生命周期结束。从寄生线程的生命周期的特征决定了它一般不能用来驱动主要的任务。

4.线程池: 1) 捕获异常:下面的例子将不能捕获异常: (1) 不加try-catch

package org.bbjava.concurrency.catching_exceptions;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExceptionThread implements Runnable {

	@Override
	public void run() {
		throw new RuntimeException();
	}
	public static void main(String []args){
		ExecutorService exec = Executors.newCachedThreadPool();
		exec.execute(new ExceptionThread());
	}

}
 

(2)加try-catch语句

package org.bbjava.concurrency.catching_exceptions;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class NativeExceptionHandling {
	public static void main(String[] args) {
		try {
			ExecutorService exec = Executors.newCachedThreadPool();
			exec.execute(new ExceptionThread());
		} catch (RuntimeException ue) {
			//This statement will not execute!
			System.out.println("Exception has been handled!");
		}
	}
}
 

综上,可知用线程池方式抛出的异常,线程池外捕获不了异常 2) 通过Thread.UncaughtExceptionHandler、ThreadFactory接口完成捕获线程池抛出的异常。eg:《thinking in java》concurrency部分。 5.使用具体的Lock对象, 问:Lock与synchronized有什么不一样么?2011-10-16:现在只知道Lock比synchronized灵活。 6.使用synchronized关键字,使用synchronized块。 在同一个对象里面synchronize的方法,会互斥。 如果这样用synchronized块,synchronized(new AnyOtherClass()){}则不会发互斥。 7.线程返回值

package org.bbjava.concurrency.producingreturnvalue;

import java.util.concurrent.Callable;

public class TaskWithResult implements Callable<String> {

	private int id;

	public TaskWithResult(int id) {
		this.id = id;
	}

	@Override
	public String call() throws Exception {
		return "result of TaskWithResult" + id;
	}

}
package org.bbjava.concurrency.producingreturnvalue;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CallableDemo {
	public static void main(String[] args) {
		ExecutorService exec = Executors.newCachedThreadPool();
		ArrayList<Future<String>> results = new ArrayList<Future<String>>();
		for (int i = 0; i < 10; i++) {
			results.add(exec.submit(new TaskWithResult(i)));
		}
		for (Future<String> fs : results)
			try {
				System.out.println(fs.get());
			} catch (ExecutionException e) {
				System.out.println(e);
				return;
			} catch (InterruptedException e) {
			} finally {
				exec.shutdown();
			}
	}
}
 

8.前面说到过实现Runnable接口创建线,其实还有一种线程就是Callable接口,该线程可以返回值,以前的Thread,Runnable都是不能返回值的。 9.Thread.join(),Thread.yeild(),Thread.sleep(),这些方法的作用。 10.你不能中断正在正在尝试获得sychronized锁的线程(task)或者是正在尝试执行I/O的线程。由其你创建一个执行I/O的任务而中断前面的I/O线程(task),那将是一种破坏,因为这意味着那样的I/O有潜在锁住多线程的程序的可能性。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。