fdsa


J. F.
Diese Karteikarten bieten einen umfassenden Überblick über die Java-Programmierung, insbesondere zu Themen wie Dateiverarbeitung, Funktionen und Sammlungen. Sie decken wichtige Konzepte wie das Arbeiten mit Pfaden, Dateien und Strings ab, sowie das Verhalten von Sammlungen und deren Sortiermethoden. Die Karteikarten sind ideal für Entwickler, die ihr Wissen in Java vertiefen möchten, insbesondere in Bezug auf die Handhabung von Sammlungen, das Sortieren und Vergleichen von Objekten sowie die Verwendung von Stream-APIs und Collectors.
Karten
496
Lernende
3
Sprache
Deutsch
Kategorie
Informatik
Stufe
Andere
Erstellt / Aktualisiert
06.12.2020 / 24.01.2021

Lernkarten

Whats the Signature of the Future-method for retrieving the result of a task, waiting the specified amount of time. If the result is not ready by time throw a checked TimeoutException

V get(long timeout, TimeUnit unit)

Whats the returntype of Future.get when its the return of a Runnable?

will be allways null

Whats the definition of the Callable interface?

@FunctionalInterface public interface Callable<V> {
  V call() throws Exception;
}

When are Runnable and Callable interchangable?

When the lambda does not throw an exception and there is no returntype

What does the ExecutorService.awaitTermination do?

waits the specified time to complete all tasks, returning sooner if all tasks finish or an InterruptedException is detected

What is the syntax of ExecutorService.awaitTermination?

boolean awaitTermination(long timeout, TimeUnit unit)

What happens if we call awaitTermination without calling shutdown before?

The thread will wait the full timeoutvalue sent with awaitTermination

Are invokeAll and invokeAny synchronous or aysnchronous?

synchronous -> wait until the result are avaiable before return control to the enclosing programm

Can we store a ScheduledExecutorService in a ExecutorService-variable?

Yes but doing so would mean we'd have to cast the object to call any scheduled methods

Whats the signature in a ScheduledExecutorService for creating and executing a Callable task after a given delay?

<V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)

Whats the signature in a ScheduledExecutorService for creating and executing a runnable task after a given delay?

ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)

Whats the signature in a ScheduledExecutorService for creating and executing a runnable task after a given initial delay, creating a new task every period value that passes?

ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)

Whats the signature in a ScheduledExecutorService for creating and executing a runnable task after the given initial delay and subsequently with the givern delay between the termination of one execution and the commencement of the next?

ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)

How to create an ExecutorService for single-threads with uses an unbounded queue and processes sequentially in the order in which they are submitted?

ExecutorService.newSingleThreadExecutor()

How to create an ExecutorService for a signle-threaded executor that can schedule commands to run after a given delay or execute periodically?

ScheduledExecutorService.newSingleThreadScheduledExecutor()

How to create an ExecutorService for creating a thread pool that creates new threads as needed but will reuse previously constructed threads when they are available?

ExecutorService.newCachedThreadPool()

How to create an ExecutorService for creating a thread pool that reuses a fixed number of threads operationg of a shared unbounded queue?

ExecutorService.newFixedThreadPool(int)

How to create an ExecutorService for creating a thread pool that can schedule commands to run after a given delay or to execute periodically?

ScheduledExecutorService.newScheduledThreadPool(int)

When is the usage of a newCachedThreadPool-Executor recommended?

For pools that require executing many short-lived asynchronous task. 

What are the three main atomic-classes?

AtomicBoolean, AtomicInteger, AtomicLong

What does an monitor support?

mutal exclusion

Is this threadsafe?
for(int i=0; i<10; i++= {
  synchronized(manager) {
    service.submit(() -> manager.incrementAndReport());
  }
}

No, we've synchronized the creation of the threads but not the execution ot the threads

What object is uses as a monitor for static synchronized methods?

The classObject, f.e. SheepManager.class
public static synchronized void printdayswork() in the class SheepManager would be the same as:
synchronized(SheepManager.class)

Which framework could we use when we want to check wheter a lock is avaiable and, if its not, perform some other task?

Lock-Framework

With the LockFramework: Can we lock any object?

No, just the ones which implement the Lockinterface

Abstract: How to use the lockFramework?

create an instance of lock that all threads have acces to. Each thread then calls lock() before it enters the protected code and calls unlock() before it exits the protected code

Whats with the fairnessparameter in a ReentrantLock?

default false but if set true then the lock will usally be granted to each thread in the order it was requested

What happens if you release a lock which you dont have?

IllegalMonitorStateException

Whats the Signature of requesting a lock and block until the lock is acquired  in the lock-interface?

public void lock()

Whats the Signature of  releasing a lock in the lock-interface?

public void unlock()

Whats the Signature of  requesting a lock and returning immedately, returning a boolean indicating whether the lock was successfully acquired in the lock-interface?

public boolean tryLock()

Whats the Signature of  requesting a lock and block up to the specified time until the lock is required, returning a boolean indicating wheter the lock was successfully acquired in the lock-interface?

public boolean tryLock(long time, TimeUnit unit)

Whats wrong here?
Lock lock = new ReentrantLock();
if(lock.tryLock()) {
  try {
    lock.lock();
  } finally {
    lock.unlock();
  }
}

Two times locked but only one released

What are the differences between ReentrantLock and a synchronized block?

the support the same futures but but ReentrantLock adds following:

  • Ability to request a lock without blocking
  • Ability to reqeust a lock while blocking for a specfied amount of time
  • create lock with fairnessproperty

What are the constructors of a CyclicBarrier?

  • CyclicBarrier(int threads)
  • CyclicBarrier(int threads, Runnable barrierAction) 

What are the most important methods of a CyclicBarrier?

  • public int await() throws InterruptedException,BrokenBarrierException
  • public int await(long timeout, TimeUnit unit) throws InterruptedException, BrokenBarrierException,TimeoutException
  • public void reset()
  • public int getNumberWaiting()

What happens if we have a fixedThreadPool with two threads but wating for 3 threads within a cyclicBarrier?

The code will hang indefinitely

When does a memorry consistency error occur?

when two threads ahve incosistent views of what should be the same data

What will happen here?
var foodData = new HashMap<String,Integer>();
foodData.put("penguin",1);
foodData.put("flamingo",2);
for(String key: foodData.keySet())
   foodData.remove(key);

A ConcurrentModificationException will be thrown

What er the concurrenct counterparts to TreeSet and ThreeMap?

ConcurrentSkipListSet and ConcurrentSkipListMap

Lernen