Java Thread 다 중 스 레 드 동기 화,잠 금,통신
14452 단어 thread
스 레 드 통신
스 레 드 그룹 과 처리 되 지 않 은 이상
Callable 과 Future
12、 , 。 Case: , : A、 、 , B、 C、 D、 , , 2 , 。 ; : synchronized (object) { // } Java , synchronized , 。 , , this : public synchronized void editByThread() { //doSomething } : A、 B、 , C、 , , , 。 Account , money , 2 money , 。 Account , synchronized 。 synchronized , 。 , , , this。 。 , 。 public synchronized void drawMoney(double money) { // } :synchronized 、 , 、 , , : A、 , ( ) 。 B、 2 : , 2 ; 。 , 。 、 , , ? , : A、 、 , B、 、 break、return , C、 、 Error、Exception, D、 、 , wait , , : A、 、 , Thread.sleep()/Thread.yield() , B、 、 , suspend , 。 suspend、resume (Lock) :Lock synchronized synchronized ,Lock , , Condition Lock 。 , , Lock , Lock 。 , :ReadWriteLock( ), , ReentrantLock( )。 Lock 、 。 class C { // private final ReentrantLock lock = new ReentrantLock(); ...... // public void method() { // lock.lock(); try { // } catch() { } finally { lock.unlock();// } } } Lock , finally 。 , Lock lock 。 synchronized , “ -> -> ” , 。 、 , , , , 。 Lock , tryLock , lockInterruptibly() , tryLock(long, timeUnit) 。 ReentrantLock , ReentrantLock ,ReentrantLock lock , lock() , unlock() , 。 2 ,JVM , 。 , , , , 。 Foo , Bar , Bar , Foo 2 , 。 Thread suspend , Java 。 13、 (1)、 : 2 , 2 。—— , , 。 2 、2 。 Object , wait、notify notifyAll , 3 Thread 。 3 , 2 : A、 synchronized , this , 3 。 B、 synchronized , synchronized , 3 : 、wait : , notify notifyAll 。 wait 3 : wait , , ; wait wait, 2 。 wait 。 、notify: 。 , 。 ( wait ), 。 、notifyAll: 。 , 。 (2)、 synchronized , Lock , , wait、notify、notifyAll 。 Lock ,Java Condition , Condition Lock , (wait-set), ,Lock ,Condition 。 Condition Lock , Lock Condition , Lock newCondition 。 Condition : 、await: wait , , Condition signal signalAll 。 await :long awaitNanos(long nanosTimeout),void awaitUninterruptibly()、awaitUntil(Date daadline) 、signal: Lock , Lock , 。 Lock , await , 。 、signalAll: Lock 。 Lock , 。 (3)、 , 3 : PipedInputStream、PipedOutputStream、PipedReader PipedWriter Pipe.SinkChannel Pipe.SourceChannel, 、 IO Channel。 : A、 new 、 B、 、 connect 2 、 C、 、 2 D、2 、 14、 ThreadGroup , ,Java Java , 。 。 , 。 , 。 , , , 。 Thread , : A、Thread(ThreadGroup group, Runnable target):target run , group B、Thread(ThreadGroup group, Runnalbe target, String name):target run , group , name C、Thread(ThreadGroup group, String name): , name, group , Thread ThreadGroup setter , getThreadGroup , getThreadGroup ThreadGroup , 。 ThreadGroup 2 : A、ThreadGroup(String name):name B、ThreadGroup(ThreadGroup parent, String name): 、 , , ThreadGroup getName , 。ThreadGroup : A、activeCount: B、interrupt: C、isDeamon: D、setDeamon: , , , 。 E、setMaxPriority: uncaughtException(Thread t, Throwable e) , Thread.UncaughtExceptionHandler Thread , :void uncaughtException(Thread t, Throwable e) t , e Thread 2 : A、staticsetDefaultUnaughtExceptionHandler(Thread.UncaughtExceptionHandler eh): B、setUncaughtExceptionHandler(Thread.UncaughtExceptionHander eh): ThreadGroup Thread.UncaughtExceptionHandler , 。 , JVM ,(setUncaughtExceptionHandler ), , 。 ,JVM uncaughtException , : A、 , uncaughtException B、 (setDefaultUnaughtExceptionHandler ), C、 System.err , 15、Callable Future Callable call , call run : A、call B、call Callable JDK5 , Runnable , Callable Thread target。 call , call , 。 call ? JDK1.5 Future Callable call , Future FutureTask , Future , Runnable — Thread target。 Future Callable : A、boolean cancel(Boolean mayInterruptlfRunning): Future Callable B、V get(): Callable call , , C、V get(long timeout, TimeUnit unit): Callable call , timeout unit 。 Callable , TimeoutException。 D、boolean isCancelled: Callable , true。 E、boolean isDone: Callable , true 、 : 、 Callable , call , call , 。 、 Callable , FutureTask Callable , FutureTask Callable call 、 FutureTask Thread target 、 、 FutureTask
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Exception in thread main java.lang. NoClassDefFoundError 오류 해결 방법즉,/home/hadoop/jarfile) 시스템은 Hello World 패키지 아래의class라는 클래스 파일을 실행하고 있다고 오인하여 시스템의 CLASSPATH 아래 (일반적으로 현재 디렉터리를 포함) Hell...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.