Java併發程式設計實踐:Callable非同步回撥Future、FutureTask用法
Callable介面類似於Runnable,從名字就可以看出來了,但是Runnable不會返回結果,並且無法丟擲返回結果的異常,而Callable功能更強大一些,被執行緒執行後,可以返回值,這個返回值可以被Future拿到。FutureTask實現了兩個介面,Runnable和Future,所以它既可以作為Runnable被執行緒執行,又可以作為Future得到Callable的返回值,那麼這個組合的使用有什麼好處呢?假設有一個很耗時的返回值需要計算,並且這個返回值不是立刻需要的話,那麼就可以使用這個組合,用另一個執行緒去計算返回值,而當前執行緒在使用這個返回值之前可以做其它的操作,等到需要這個返回值時,再通過Future得到。
Future特性
需要明確一點:java.util.concurrent.Future 是一個非同步回撥介面。
Future介面原始碼
/* * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * */ /* * * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ package java.util.concurrent; /** * A <tt>Future</tt> represents the result of an asynchronous * computation. Methods are provided to check if the computation is * complete, to wait for its completion, and to retrieve the result of * the computation. The result can only be retrieved using method * <tt>get</tt> when the computation has completed, blocking if * necessary until it is ready. Cancellation is performed by the * <tt>cancel</tt> method. Additional methods are provided to * determine if the task completed normally or was cancelled. Once a * computation has completed, the computation cannot be cancelled. * If you would like to use a <tt>Future</tt> for the sake * of cancellability but not provide a usable result, you can * declare types of the form {@code Future<?>} and * return <tt>null</tt> as a result of the underlying task. * * <p> * <b>Sample Usage</b> (Note that the following classes are all * made-up.) <p> * <pre> {@code * interface ArchiveSearcher { String search(String target); } * class App { * ExecutorService executor = ... * ArchiveSearcher searcher = ... * void showSearch(final String target) * throws InterruptedException { * Future<String> future * = executor.submit(new Callable<String>() { * public String call() { * return searcher.search(target); * }}); * displayOtherThings(); // do other things while searching * try { * displayText(future.get()); // use future * } catch (ExecutionException ex) { cleanup(); return; } * } * }}</pre> * * The {@link FutureTask} class is an implementation of <tt>Future</tt> that * implements <tt>Runnable</tt>, and so may be executed by an <tt>Executor</tt>. * For example, the above construction with <tt>submit</tt> could be replaced by: * <pre> {@code * FutureTask<String> future = * new FutureTask<String>(new Callable<String>() { * public String call() { * return searcher.search(target); * }}); * executor.execute(future);}</pre> * * <p>Memory consistency effects: Actions taken by the asynchronous computation * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a> * actions following the corresponding {@code Future.get()} in another thread. * * @see FutureTask * @see Executor * @since 1.5 * @author Doug Lea * @param <V> The result type returned by this Future's <tt>get</tt> method */ public interface Future<V> { /** * Attempts to cancel execution of this task. This attempt will * fail if the task has already completed, has already been cancelled, * or could not be cancelled for some other reason. If successful, * and this task has not started when <tt>cancel</tt> is called, * this task should never run. If the task has already started, * then the <tt>mayInterruptIfRunning</tt> parameter determines * whether the thread executing this task should be interrupted in * an attempt to stop the task. * * <p>After this method returns, subsequent calls to {@link #isDone} will * always return <tt>true</tt>. Subsequent calls to {@link #isCancelled} * will always return <tt>true</tt> if this method returned <tt>true</tt>. * * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this * task should be interrupted; otherwise, in-progress tasks are allowed * to complete * @return <tt>false</tt> if the task could not be cancelled, * typically because it has already completed normally; * <tt>true</tt> otherwise */ boolean cancel(boolean mayInterruptIfRunning); /** * Returns <tt>true</tt> if this task was cancelled before it completed * normally. * * @return <tt>true</tt> if this task was cancelled before it completed */ boolean isCancelled(); /** * Returns <tt>true</tt> if this task completed. * * Completion may be due to normal termination, an exception, or * cancellation -- in all of these cases, this method will return * <tt>true</tt>. * * @return <tt>true</tt> if this task completed */ boolean isDone(); /** * Waits if necessary for the computation to complete, and then * retrieves its result. * * @return the computed result * @throws CancellationException if the computation was cancelled * @throws ExecutionException if the computation threw an * exception * @throws InterruptedException if the current thread was interrupted * while waiting */ V get() throws InterruptedException, ExecutionException; /** * Waits if necessary for at most the given time for the computation * to complete, and then retrieves its result, if available. * * @param timeout the maximum time to wait * @param unit the time unit of the timeout argument * @return the computed result * @throws CancellationException if the computation was cancelled * @throws ExecutionException if the computation threw an * exception * @throws InterruptedException if the current thread was interrupted * while waiting * @throws TimeoutException if the wait timed out */ V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }
Callable返回Future示例
import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; /** * Callable的Future用法 * @package .CallableDemo * @date 2017年4月5日 下午2:53:18 * @author pengjunlin * @comment * @update */ public class CallableFuture { /** * @param args * @throws Exception * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException, Exception { // TODO Auto-generated method stub ExecutorService exec = Executors.newCachedThreadPool(); // Future是一個介面,該介面用來返回非同步的結果。 Future<String> st = exec.submit(new TaskCallable()); /* 同步結果,並且設定超時時間 */ System.out.println(st.get(10000, TimeUnit.MILLISECONDS)); System.out.println("finished"); } } class TaskCallable implements Callable<String> { public String call() throws Exception { // TODO Auto-generated method stub Thread.sleep(1000); return "callstatus=OK"; } }
FutureTask
FutureTask實現了java.util.concurrent.RunnableFuture<V>介面,實際上實現了Runnable和 Future<V>兩個介面。
FutureTask原始碼
/*
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*/
/*
*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*/
package java.util.concurrent;
import java.util.concurrent.locks.LockSupport;
/**
* A cancellable asynchronous computation. This class provides a base
* implementation of {@link Future}, with methods to start and cancel
* a computation, query to see if the computation is complete, and
* retrieve the result of the computation. The result can only be
* retrieved when the computation has completed; the {@code get}
* methods will block if the computation has not yet completed. Once
* the computation has completed, the computation cannot be restarted
* or cancelled (unless the computation is invoked using
* {@link #runAndReset}).
*
* <p>A {@code FutureTask} can be used to wrap a {@link Callable} or
* {@link Runnable} object. Because {@code FutureTask} implements
* {@code Runnable}, a {@code FutureTask} can be submitted to an
* {@link Executor} for execution.
*
* <p>In addition to serving as a standalone class, this class provides
* {@code protected} functionality that may be useful when creating
* customized task classes.
*
* @since 1.5
* @author Doug Lea
* @param <V> The result type returned by this FutureTask's {@code get} methods
*/
public class FutureTask<V> implements RunnableFuture<V> {
/*
* Revision notes: This differs from previous versions of this
* class that relied on AbstractQueuedSynchronizer, mainly to
* avoid surprising users about retaining interrupt status during
* cancellation races. Sync control in the current design relies
* on a "state" field updated via CAS to track completion, along
* with a simple Treiber stack to hold waiting threads.
*
* Style note: As usual, we bypass overhead of using
* AtomicXFieldUpdaters and instead directly use Unsafe intrinsics.
*/
/**
* The run state of this task, initially NEW. The run state
* transitions to a terminal state only in methods set,
* setException, and cancel. During completion, state may take on
* transient values of COMPLETING (while outcome is being set) or
* INTERRUPTING (only while interrupting the runner to satisfy a
* cancel(true)). Transitions from these intermediate to final
* states use cheaper ordered/lazy writes because values are unique
* and cannot be further modified.
*
* Possible state transitions:
* NEW -> COMPLETING -> NORMAL
* NEW -> COMPLETING -> EXCEPTIONAL
* NEW -> CANCELLED
* NEW -> INTERRUPTING -> INTERRUPTED
*/
private volatile int state;
private static final int NEW = 0;
private static final int COMPLETING = 1;
private static final int NORMAL = 2;
private static final int EXCEPTIONAL = 3;
private static final int CANCELLED = 4;
private static final int INTERRUPTING = 5;
private static final int INTERRUPTED = 6;
/** The underlying callable; nulled out after running */
private Callable<V> callable;
/** The result to return or exception to throw from get() */
private Object outcome; // non-volatile, protected by state reads/writes
/** The thread running the callable; CASed during run() */
private volatile Thread runner;
/** Treiber stack of waiting threads */
private volatile WaitNode waiters;
/**
* Returns result or throws exception for completed task.
*
* @param s completed state value
*/
@SuppressWarnings("unchecked")
private V report(int s) throws ExecutionException {
Object x = outcome;
if (s == NORMAL)
return (V)x;
if (s >= CANCELLED)
throw new CancellationException();
throw new ExecutionException((Throwable)x);
}
/**
* Creates a {@code FutureTask} that will, upon running, execute the
* given {@code Callable}.
*
* @param callable the callable task
* @throws NullPointerException if the callable is null
*/
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}
/**
* Creates a {@code FutureTask} that will, upon running, execute the
* given {@code Runnable}, and arrange that {@code get} will return the
* given result on successful completion.
*
* @param runnable the runnable task
* @param result the result to return on successful completion. If
* you don't need a particular result, consider using
* constructions of the form:
* {@code Future<?> f = new FutureTask<Void>(runnable, null)}
* @throws NullPointerException if the runnable is null
*/
public FutureTask(Runnable runnable, V result) {
this.callable = Executors.callable(runnable, result);
this.state = NEW; // ensure visibility of callable
}
public boolean isCancelled() {
return state >= CANCELLED;
}
public boolean isDone() {
return state != NEW;
}
public boolean cancel(boolean mayInterruptIfRunning) {
if (state != NEW)
return false;
if (mayInterruptIfRunning) {
if (!UNSAFE.compareAndSwapInt(this, stateOffset, NEW, INTERRUPTING))
return false;
Thread t = runner;
if (t != null)
t.interrupt();
UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED); // final state
}
else if (!UNSAFE.compareAndSwapInt(this, stateOffset, NEW, CANCELLED))
return false;
finishCompletion();
return true;
}
/**
* @throws CancellationException {@inheritDoc}
*/
public V get() throws InterruptedException, ExecutionException {
int s = state;
if (s <= COMPLETING)
s = awaitDone(false, 0L);
return report(s);
}
/**
* @throws CancellationException {@inheritDoc}
*/
public V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
if (unit == null)
throw new NullPointerException();
int s = state;
if (s <= COMPLETING &&
(s = awaitDone(true, unit.toNanos(timeout))) <= COMPLETING)
throw new TimeoutException();
return report(s);
}
/**
* Protected method invoked when this task transitions to state
* {@code isDone} (whether normally or via cancellation). The
* default implementation does nothing. Subclasses may override
* this method to invoke completion callbacks or perform
* bookkeeping. Note that you can query status inside the
* implementation of this method to determine whether this task
* has been cancelled.
*/
protected void done() { }
/**
* Sets the result of this future to the given value unless
* this future has already been set or has been cancelled.
*
* <p>This method is invoked internally by the {@link #run} method
* upon successful completion of the computation.
*
* @param v the value
*/
protected void set(V v) {
if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
outcome = v;
UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
finishCompletion();
}
}
/**
* Causes this future to report an {@link ExecutionException}
* with the given throwable as its cause, unless this future has
* already been set or has been cancelled.
*
* <p>This method is invoked internally by the {@link #run} method
* upon failure of the computation.
*
* @param t the cause of failure
*/
protected void setException(Throwable t) {
if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
outcome = t;
UNSAFE.putOrderedInt(this, stateOffset, EXCEPTIONAL); // final state
finishCompletion();
}
}
public void run() {
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
result = c.call();
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
if (ran)
set(result);
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
int s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}
/**
* Executes the computation without setting its result, and then
* resets this future to initial state, failing to do so if the
* computation encounters an exception or is cancelled. This is
* designed for use with tasks that intrinsically execute more
* than once.
*
* @return true if successfully run and reset
*/
protected boolean runAndReset() {
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return false;
boolean ran = false;
int s = state;
try {
Callable<V> c = callable;
if (c != null && s == NEW) {
try {
c.call(); // don't set result
ran = true;
} catch (Throwable ex) {
setException(ex);
}
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
return ran && s == NEW;
}
/**
* Ensures that any interrupt from a possible cancel(true) is only
* delivered to a task while in run or runAndReset.
*/
private void handlePossibleCancellationInterrupt(int s) {
// It is possible for our interrupter to stall before getting a
// chance to interrupt us. Let's spin-wait patiently.
if (s == INTERRUPTING)
while (state == INTERRUPTING)
Thread.yield(); // wait out pending interrupt
// assert state == INTERRUPTED;
// We want to clear any interrupt we may have received from
// cancel(true). However, it is permissible to use interrupts
// as an independent mechanism for a task to communicate with
// its caller, and there is no way to clear only the
// cancellation interrupt.
//
// Thread.interrupted();
}
/**
* Simple linked list nodes to record waiting threads in a Treiber
* stack. See other classes such as Phaser and SynchronousQueue
* for more detailed explanation.
*/
static final class WaitNode {
volatile Thread thread;
volatile WaitNode next;
WaitNode() { thread = Thread.currentThread(); }
}
/**
* Removes and signals all waiting threads, invokes done(), and
* nulls out callable.
*/
private void finishCompletion() {
// assert state > COMPLETING;
for (WaitNode q; (q = waiters) != null;) {
if (UNSAFE.compareAndSwapObject(this, waitersOffset, q, null)) {
for (;;) {
Thread t = q.thread;
if (t != null) {
q.thread = null;
LockSupport.unpark(t);
}
WaitNode next = q.next;
if (next == null)
break;
q.next = null; // unlink to help gc
q = next;
}
break;
}
}
done();
callable = null; // to reduce footprint
}
/**
* Awaits completion or aborts on interrupt or timeout.
*
* @param timed true if use timed waits
* @param nanos time to wait, if timed
* @return state upon completion
*/
private int awaitDone(boolean timed, long nanos)
throws InterruptedException {
final long deadline = timed ? System.nanoTime() + nanos : 0L;
WaitNode q = null;
boolean queued = false;
for (;;) {
if (Thread.interrupted()) {
removeWaiter(q);
throw new InterruptedException();
}
int s = state;
if (s > COMPLETING) {
if (q != null)
q.thread = null;
return s;
}
else if (s == COMPLETING) // cannot time out yet
Thread.yield();
else if (q == null)
q = new WaitNode();
else if (!queued)
queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
q.next = waiters, q);
else if (timed) {
nanos = deadline - System.nanoTime();
if (nanos <= 0L) {
removeWaiter(q);
return state;
}
LockSupport.parkNanos(this, nanos);
}
else
LockSupport.park(this);
}
}
/**
* Tries to unlink a timed-out or interrupted wait node to avoid
* accumulating garbage. Internal nodes are simply unspliced
* without CAS since it is harmless if they are traversed anyway
* by releasers. To avoid effects of unsplicing from already
* removed nodes, the list is retraversed in case of an apparent
* race. This is slow when there are a lot of nodes, but we don't
* expect lists to be long enough to outweigh higher-overhead
* schemes.
*/
private void removeWaiter(WaitNode node) {
if (node != null) {
node.thread = null;
retry:
for (;;) { // restart on removeWaiter race
for (WaitNode pred = null, q = waiters, s; q != null; q = s) {
s = q.next;
if (q.thread != null)
pred = q;
else if (pred != null) {
pred.next = s;
if (pred.thread == null) // check for race
continue retry;
}
else if (!UNSAFE.compareAndSwapObject(this, waitersOffset,
q, s))
continue retry;
}
break;
}
}
}
// Unsafe mechanics
private static final sun.misc.Unsafe UNSAFE;
private static final long stateOffset;
private static final long runnerOffset;
private static final long waitersOffset;
static {
try {
UNSAFE = sun.misc.Unsafe.getUnsafe();
Class<?> k = FutureTask.class;
stateOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("state"));
runnerOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("runner"));
waitersOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("waiters"));
} catch (Exception e) {
throw new Error(e);
}
}
}
Callable返回FutureTask示例
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* Callable的MyFutureTask用法
*
* @package .MyFutureTask
* @date 2017年4月5日 下午2:56:50
* @author pengjunlin
* @comment
* @update
*/
public class MyFutureTask {
public static void main(String[] args) {
Callable<int[]> primeCallable = new PrimeCallable(1000);
FutureTask<int[]> primeTask = new FutureTask<int[]>(primeCallable);
Thread t = new Thread(primeTask);
t.start();
try {
// 假設現在做其他事情
Thread.sleep(5000);
// 回來看看質數找好了嗎
if (primeTask.isDone()) {
int[] primes = primeTask.get();
for (int prime : primes) {
System.out.print(prime + " ");
}
System.out.println();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
class PrimeCallable implements Callable<int[]> {
private int max;
public PrimeCallable(int max) {
this.max = max;
}
public int[] call() throws Exception {
int[] prime = new int[max + 1];
List<Integer> list = new ArrayList<Integer>();
for (int i = 2; i <= max; i++)
prime[i] = 1;
for (int i = 2; i * i <= max; i++) { // 這裡可以改進
if (prime[i] == 1) {
for (int j = 2 * i; j <= max; j++) {
if (j % i == 0)
prime[j] = 0;
}
}
}
for (int i = 2; i < max; i++) {
if (prime[i] == 1) {
list.add(i);
}
}
int[] p = new int[list.size()];
for (int i = 0; i < p.length; i++) {
p[i] = list.get(i).intValue();
}
return p;
}
}
相關推薦
Java併發程式設計實踐:Callable非同步回撥Future、FutureTask用法
Callable介面類似於Runnable,從名字就可以看出來了,但是Runnable不會返回結果,並且無法丟擲返回結果的異常,而Callable功能更強大一些,被執行緒執行後,可以返回值,這個返回值可以被Future拿到。FutureTask實現了兩個介面,Runnable
JAVA併發程式設計(五):建立執行緒的第三種方式:實現Callable介面
眾所周知建立執行緒的方式有兩種:1.繼承Thread類。2.實現Runnable介面。從jdk1.5開始,提供了另一種建立執行緒的方式。今天我們就來看看這第三種方式:實現Callable介面 一、Callable與Runnable 我們直接來看一個使用C
《JAVA併發程式設計實踐》戈茨.掃描版.pd
書籍簡介: 《JAVA併發程式設計實踐》中,這些便利工具的創造者不僅解釋了它們究竟如何工作、如何使用,同時,還闡釋了創造它們的原因,及其背後的設計模式。隨著多核處理器的普及,使用併發成為構建高效能應用程式的關鍵。Java 5以及6在開發併發程式取得了顯著的進步,提高了Java虛擬機器的效能
Java併發程式設計 - Executor,Executors,ExecutorService, CompletionServie,Future,Callable
一、Exectuor框架簡介 Java從1.5版本開始,為簡化多執行緒併發程式設計,引入全新的併發程式設計包:java.util.concurrent及其併發程式設計框架(Executor框架)。 Executor框架是指ja
Java併發程式設計學習:volatile關鍵字解析
轉載:https://www.cnblogs.com/dolphin0520/p/3920373.html 寫的非常棒,好東西要分享一下 Java併發程式設計:volatile關鍵字解析 volatile這個關鍵字可能很多朋友都聽說過,或許也都用過。在Java 5之前,它是一個備受
《Java併發程式設計實踐——第七章(取消和關閉)》
取消和關閉 中斷——一種協作機制,使執行緒能夠要求另一個執行緒停止當前工作。 7.1 任務取消 當外部程式碼能夠在活動自然完成之前,把它改為完成狀態,那個這個活動被稱為可取消的。 7.1.1 中斷 執行緒中斷方法: 中斷並不會真正中斷一個正在執行的執行緒,它僅
《Java併發程式設計實踐——第四章(組合物件)》
組合物件 我們不希望為了獲得執行緒安全而去每次分析記憶體訪問;而希望執行緒安全的元件能夠以安全的方式組合成更大的元件或程式。 4.1 設計執行緒安全的類 4.1.1 收集同步需求 4.1.2 狀態依賴的操作 4.1.3 狀態所有權 4.2
《Java併發程式設計實踐——第三章(共享物件)》
共享物件## 編寫正確的併發程式的關鍵在於對共享的、可變的物件狀態進行訪問管理。 上一章使用同步來避免多個執行緒在同一時間訪問同一資料。 同步還有另外的方面:記憶體可見性。 3.1 可見性 public class NoVisibility { private static
《Java併發程式設計實踐——第一章(介紹)、第二章(執行緒安全)》
介紹## 1.1 併發的簡短歷史 相同的關注點(資源利用,公平和方便) 不僅促進了程序的發展,也促進了執行緒的發展、 執行緒允許程式控制流的多重分支同時存在於一個程序。它們共享程序範圍內的資源,比如記憶體和檔案控制代碼,但是執行緒有自己的程式計數器、棧、和本地變數。 1.2
Java併發程式設計實戰:閉鎖CountDownLatch,柵欄CyclicBarrier與訊號量Semaphore
整體上對三個概念進行一個說明: CountDownLatch和CyclicBarrier都能夠實現執行緒之間的等待,只不過它們側重點不同: CountDownLatch是閉鎖,相當於一扇門:在閉鎖達到結束狀態之前,這扇門一直是關閉的,並且沒有任何執行緒能夠通過,當到達結束
《JAVA併發程式設計實踐中文完整版》電子書附下載連結+30個總結JVM虛擬機器的技術文排版好(收藏版)
技術書閱讀方法論 一.速讀一遍(最好在1~2天內完成) 人的大腦記憶力有限,在一天內快速看完一本書會在大腦裡留下深刻印象,對於之後複習以及總結都會有特別好的作用。 對於每一章的知識,先閱讀標題,弄懂大概講的是什麼主題,再去快速看一遍,不懂也沒有關係,但是一定要在不懂的
《JAVA併發程式設計實踐》第三章物件的共享
1 怎麼理解發布和逸出? 轉自別人的回答講的很詳細 點選這裡 還有這裡 2.如何構造一個安全的物件? 不可變的物件一定是執行緒安全的 在多執行緒訪問這個不可變的物件時,物件的例項域都是固定不變的,也就不存在多執行緒
《JAVA併發程式設計實踐》第二章 執行緒安全性
1.多執行緒環境為什麼會出現問題? 由於競態條件的存在。 競態條件:基於一種可能失效的觀察結果來做出判斷或執行某個計算 競態條件分兩種: - **先檢查後執行**
Java併發程式設計實踐筆記之——加鎖機制(Locking)
多個狀態變數的一致性保持 當執行緒安全的不變性條件中涉及多個變數時,並且各個變數之間不是彼此獨立,某個變數對其他變數的值產生約束。那當更新一個變數時,要在同一個原子操作中更新其他的變數內建鎖(Intrinsic Locks) Java提供了同步程式碼塊(Synchroni
Java併發程式設計三:併發(Concurrent)與並行(Parallel)的區別(一)
併發(Concurrent)與並行(Parallel)是一個大家比較容易混淆的概念。大家在解釋併發與並行的時候一般這樣說: 多執行緒是併發執行的; 多核CPU是並行執行的,單核CPU是不可以不行執行的; 以上說法也是可以理解的,大家都是基於場景來描述的。
《Java併發程式設計實踐》筆記1——併發程式設計基礎
1.執行緒安全定義: 當多個執行緒訪問一個類時,如果不用考慮這些執行緒在執行時環境下的排程和交替執行,並且不需要額外的同步及在呼叫方程式碼不必做其他的協調,這個類的行為仍然是正確的,那麼這個類就被稱之為是執行緒安全的。簡言之對於執行緒安全類的例項進行順序或併發的一系列操作,
Java併發程式設計規則:不可變物件永遠是執行緒安全的
建立後狀態不能被修改的物件叫作不可變物件。不可變物件天生就是執行緒安全的。它們的常量(變數)是在建構函式中建立的,既然它們的狀態無法被修改,那麼這些常量永遠不會被改變——不可變物件永遠是執行緒安全的。 不可變性的理解: 無論是Java語言規範還是Java儲存模型都沒有對不可
《java併發程式設計實踐》1-3章要點總結
名詞解析 1. 上下文切換 排程程式臨時掛起當前執行的執行緒時,另一個執行緒開始執行。也就是執行緒切換,在應用程式中很常見,帶來的系統開銷巨大。 2. 原子性 程式可以作為單獨的、不可分割的一次操作執行,稱為原子操作,具有原子性,例如賦值操作:a
《JAVA併發程式設計實踐》第四章物件的組合
1.如何寫出一個執行緒安全的類? 基本要素: 找出構成物件狀態的所有變數 找出約束狀態變數的不變性條件(取值範圍 前驗條件或者後驗條件) 建立物件狀態的併發管理策略 實際中的程式碼 訂單類包含自身的狀態和商品的引用 public
Java併發程式設計規則:原子變數實現執行緒安全
判定規則: 如果一個類中存在變數,並且此變數的操作不是原子操作,那麼這個類就是非執行緒安全的類。線上程產生競爭條件的情況下,多執行緒訪問導致原子性不可保證。 競爭條件產生的原因: 當計算的正確性依賴於執行時中相關的時序或多執行緒的交替時,會產生競爭條件。多執行緒情況下,執行