midturm Flashcards
How many thread objects execute at once per CPU
At any moment, at most one thread object is executing per CPU
what are the behaviours of thread
Thread: defines actual behaviour
What is the thread state lifecycle
- new
- ready
- running
- waiting
- sleeping
- blocked
- dead
what are the behaviours of interface
Interface: defines desired behaviour
How can you put a thread on hold
- yield ()
- sleep ()
- join ()
- wait ()
- suspend ()
Thread.yield()
Static method
Causes the thread executing the method to allow another thread of the same priority to run
Should be used for non-timesliced systems
When does a thread die?
Threads die when run() method returns
how to check if the thread is still viable
isAlive()
what arguments does join() take?
Join ([timeout])
join()
Join method causes current thread to wait until the thread of the threadObject on which the join method is called terminates (threadObject.join();)
Join lets another thread finish or gives it a limited amount of time to finish
Thread.sleep()
Static method
Causes thread to pause for a fixed period of time
what arguments does thread.sleep() take?
Thread.sleep (long milis, [int nanos])
thread.interrupt()
allows another object to interrupt the sleep period
What exceptions does thread.interrupt() throw?
interruptedException – if another thread has interrupted the current thread
IllegalArgumentException – millis is negative or value of nanos is not in the range 0-999999
Blocked
All java I/O methods step out of the running state while paused for response, said to be blocked
ThreadGroup
Can be dealt with as a unit
Hierarchical relationships – parent/child
Method calls sent to a parent group also sent to all its children
Construct your threads with the ThreadGroupName in the constructor
What does the RMI client do?
Obtains a remote reference to one or more remote objects
Invokes methods on those objects
Synchronization problems
Arises when multiple threads access shared data (producer/consumer problem)
Producer fills the buffer/Consumer empties the buffer
RMI interface support layers:
client/server
Stub/skeleton layer
remote reference layer
transport layer
Object monitor lock
Every object has a flag associated with it – called monitor lock
Used with the synchronized key word
All methods accessing shared data should synchronize on the same lock object
Stub/skeleton layer
responsible for managing the remote object interface between the client and server
How many synchronized methods can be active on an object at once?
Only 1 synchronized method may be active on an object at once
All other threads trying to acquire the monitor lock will block
what does RMI involve
RMI involve client and server sides
Remote reference layer
responsible for managing the “liveliness” of the remote objects. Manages the communication between the client/server and virtual machines
how to RUN the rmiregistry (2 ways)
1 – CMD – run the rmiregistry command on the server
2 – server calls LocateRegistry.createRegistry(portNum)
how are RMI object parameters passed?
Object parameters are passed by value (object not the reference)
what does the RMI server do?
Create some remote objects (local to server)
Make references to those objects available on the network
Wait for clients to invoke methods on those objects
how are RMI objects sent?
Object serialization is used to send parameters
Object de-serialization is used to reconstitute the parameters in the destination JVM
Transport layer
actual network/communication layer that is used to send the information between the client and server over the wire. Currently TCP/IP based. Uses serialization.
what is the RMI registry
RMI Registry runs on each machine that hosts remote service objects and accepts queries for services, by default on port 1099 (Registry can be run on a non-default port if desired)
how to start the RMI registry (2 ways)
1 – rmiregistry is a program in the JSDK bin directory, just run it on the server
2 –LocateRegistry.createRegistry(portNum) can be called in the server
THEN:
Start the RMI server
Start the RMI client
is multithreading platform dependent or system dependent?
Multithreading is platform dependent (windows etc.)
Synchronized as a block modifier syntax:
Synchronized(someObject){…}
multithreading
you can specify that applications contain threads of execution. Each thread designates a portion of a program that may execute concurrently with other threads.
Synchronized as a method modifier syntax:
Public synchronized push (char c)
Thread synchronization: How many threads does monitor allow to execute at a time?
Monitor allows 1 thread at a time to execute a synchronized method on the object
Synchronized as a method modifier: Public synchronized push (char c)
Only 1 thread can be active in the objects synchronized methods
Other threads will block until active thread returns from method
When 1 thread is executing in a synchronized method, ALL other threads are locked out of ALL synchronized methods of that object
How do you code remote, runnable and thread?
Extends Remote
Implements Runnable
Extends Thread
Thread synchronization: what happens to the other threads while they wait for the thread to finish executing?
All other threads block as the try to invoke the objects synchronized methods
Daemon threads:
Threads that run for the benefit of other threads
Run in the background
Do not prevent a program from terminating
Synchronized as a block modifier Synchronized(someObject){…}:
Any thread trying to execute the code “…” in that block must obtain the monitor lock of someObject
Only 1 thread at a time can hold the monitor lock of an object
Other threads will block until monitor lock has released when the current thread leaves the block
3 ways to create a thread
- pass object that implements Runnable to the Thread class constructor and start a new thread: start method
- extend the Thread class: in a new class MyThread, override run, instantiate a new MyThread object, start it. ONLY use 2 when you need to override other Thread methods besides run.
- use an executor service: java concurrent package
threadObject.start() method:
places thread into runnable state, viable for scheduling for execution by the JVM thread scheduler. Now the thread is runnable, ELIGIBLE to run. Start() method returns to its caller then continues to run concurrently with the newly launched thread.
Thread synchronization: When synchronized method finishes:
lock on object is released, monitor lets the highest-priority ready thread attempting to invoke a synchronized method proceed
can executors reuse existing threads or do they create new threads for each call?
Executors can reuse existing threads and can improve performance by optimizing the number of threads.
Create a daemon thread:
setDaemon(true) method, when only daemonds thread exist in program – program exits.
Executor framework:
Used to manage many Runnable’s. creates and manages a group of threads called a thread pool to execute Runnable’s.
ExecutorService Methods:
Execute(runnableInstance)
Submit(runnableInstance or callableInstance)
invokeAny(collection_of_Callables, [long timeout, TimeUnit unit])
invokeAll(collection_of_callables, [long timeout, TimeUnit unit])
Shutdown()
shutdownNow()
awaitTermination(long timeout, TimeUnit unit)
CORBA definition
Common object request broker architecture
ExecutorService interface:
ExecutorService interface extends executor and declares methods for managing the life cycle of executor
What is CORBA
CORBA (Common object request broker architecture): enables software components written in multiple computer languages and running on multiple computers work together
When do you set a Daemon thread?
If thread is daemon it must be set as such before the start method is called
what does newCachedThreadPool do and return?
Executors method newCachedThreadPool returns an executorService that creates new threads as they’re needed by the application.
what type of thread is the garbage collector?
Garbage collector is a daemon thread (setDaemon(true);) – false argument means that the thread is not a daemon thread
ExecutorService.shutdown()
ExecutorService method shutdown notified the executorService to stop accepting new tasks but continues executing tasks that have already been submitted.
Java RMI:
Java only, interfaces specified in Java, distributed garbage collection integrated with local collectors, generally simpler
Executors factory methods:
newCachedThreadPool
newFixedThreadPool(nThreads)
newScheduledThreadPool(nThreads)
newWorkStealingPool([nThreads])
Echo client java step 3 explained:
3 – perform the processing: get string from user, send string to server, read string object from server, display string to user
Execute(runnableInstance)
returns void so you cannot process any results; best for fire and forget tasks
Echo server java step 1 explained:
1 - Creating a server socket: ServerSocket server = new ServerSocket(portNumber, backlog); - port is the port or application number, backlog is the number of clients that can wait to connect ( default backlog is 50)
invokeAll(collection_of_callables, [long timeout, TimeUnit unit])
returns List of Future<>’s representing results of all tasks after all finish (or times out if timeout given)
Echo client in Java Steps:
1 – create a socket
2 – get the socket’s IO streams
3 – perform the processing
4 – close the connection