java 2 3
fdsa
fdsa
Set of flashcards Details
Flashcards | 496 |
---|---|
Language | Deutsch |
Category | Computer Science |
Level | Other |
Created / Updated | 06.12.2020 / 24.01.2021 |
Weblink |
https://card2brain.ch/box/20201206_java_2_3
|
Embed |
<iframe src="https://card2brain.ch/box/20201206_java_2_3/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>
|
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
Whats special about CopyOnWriteArrayList and CopyOnWriteArraySet?
tey copy all of their elements to a new underlying structure anytime an element is added, modified or removed from the collection
What does "modifing" mean in the context of CopyOnWriteArrayList and copyOnWriteArraySet?
when the reference in the collection is changed
What happens here?
List<Integer> favNumbers = new CopyOnWriteArrayList<>(List.of(4,3,42));
for(var n: favNumbers) {
System.out.print(n + " ");
favNumbers.add(9);
}
System.out.println();
System.out.println("Size: " + favNumbers.size());
4 3 42
Size 6
What is special about BlockingQueue?
Its like a regular queue but it includes Methods that will wait a specfic amount of time to complete an operation
Whats the syntax for adding an item to the queue, wating the specified time and returning false if the time elapses before space is availiable in BlockingQueue?
boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
Whats the syntax for retrieving and removing an item from the queue, waiting for the specified time and returing null if the time elapses before the item is availiable in BlockingQueue?
E poll(long timeout, TimeUnit unt) throws InterruptedException
Whats a difference between concurrent and snychronized collections regarding the exceptions?
unlike the concurrent collections the snychronized collections also throw an exception if they are modified witihn an iterator by a single thread
How to create a parallelStream form an existing stream?
Calling parallel()
How to create a parallel stream from a collection?
calling parallelStream()
When Stream.concat is called on two parallel streams, is the result still parallel?
yes
When Stream.flatmap is called on two parallel streams, is the result still parallel?
No
What does parallel decomposition mean?
process of taking a task, breaking it up into smaller pieces that can be performed concurrently and then reassembling the results
Does forEachOrdered makes the whole stream serialized?
No
What does the streamoperation reduce() do?
combines a stream into a single object
What is the syntax of stream.reduce with parallel streams?
<U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner)
Why is this problematic?
System.out.println(List.of(1,2,3,4,5,6)
.parallelStream()
.reduce(0, (a,b) -> (a-b)));
It may output -21,3 or some other value
Whats the syntax of Stream.collect with parallel Streams?
<R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R,R> combiner)
What are the requirements to execute Stream.collect in parallell?
- the stream is parallel
- parameter of collect ooperation has the Characteristics.CONCURRENT
- either the stream is unordered or the collector has the characteristic Characteristics.UNORDERED
What does "transitiv" do witihn a module-info.java?
Indicates the module and that all modules that use this module are dependent on another module
Which types of modules exists?
named, unnamed, automatic
What is the characteristics of a named module?
has the name inside the module-info and is on the module path
What is the characteristics of an automatic module?
is on the module path, has no module-info, is simply a regular jar-file on the module-path and treated as a module
How does code which is referencing an automatic module treat it?
As if there is a module-ionfo file and exports all packages. it determines the modulename
What are the two ways of determining the name of a automatic module?
* set a property called Automatic-Module-Name in the MANIFEST.MF file
* Java determines it by passing it off the filename of the jar file