Practical OOP using Java Training @ Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
-
Upload
violet-blake -
Category
Documents
-
view
216 -
download
0
Transcript of Practical OOP using Java Training @ Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
Course objectives• Java programming language basics• Object Oriented Programming concepts and using
OOP in real-life applications• Developing desktop (GUI) applications using Java
SE• Introduction to relational database concepts• Introduction to web application programming
using Java EE
Concurrency• Systems can do more than one thing at a time.– Playing music while displaying video.– Respond to keyboard and mouse while printing a
document.
• Level of multi tasking – Processes: Self contained execution environment.
Use IPC to inter-process communication
Concurrency (Continued)• Level of multi tasking (continued)– Threads: • Light weigh process• Takes less resources • Use shared resources• Context switching inexpensive• Inter-thread communication usually inexpensive• Every process has at least one process
Concurrency in JAVA• Multi-thread support in java is an essential
feature• System thread: By JVM to mange memory,
signalling etc• Typical java program has main thread (main
function)• Main thread can create/manage many more
thread
Concurrency in JAVA (Continued)• Defining and Starting a Thread– Provide a Runnable object by implementing the
Runnable interface.– Subclass Thread. The Thread class itself
implements Runnable.– Invoke Thread.start() in order to start the new
thread.
Concurrency in JAVA (Continued)• Thread objects– Each thread is associated with an instance of the
class Thread.
• Two basic strategies for using Thread– To directly control thread creation and
management– To abstract thread management from the rest of
your application via executor
Concurrency in JAVA (Continued)• Pausing a Thread– Invoke by Thread.sleep(miliseconds) to suspend
execution for a specified period.
• Interrupts– Stop what is doing and do something else (mostly
exit)– Invoked by thread’s intrrupt() method – Caught by InterruptedException or interrupted
method
Concurrency in JAVA (Continued)• Thread join() method– join method allows one thread to wait for the
completion of another– This causes current method to pause execution– Starts again once target thread terminates– Subject to InterruptedException
Synchronization• Two issues in using shared resources:– Thread Interference (c++/c--)
1. Thread A: Retrieve c.2. Thread B: Retrieve c.3. Thread A: Increment retrieved value; result is 1.4. Thread B: Decrement retrieved value; result is -1.5. Thread A: Store result in c; c is now 1.6. Thread B: Store result in c; c is now -1.
Synchronization– Memory Consistency Errors• int counter = 0; // Shared variable• counter++; // incremented by thread A• System.out.println(counter); // sometime printed by
thread B
Synchronization…• Use keyword synchronized - A thread cannot
acquire a lock owned by another thread.– Synchronized methods • Lock using an object
– Synchronized blocks• Lock using an object• Lock using a class
Synchronization…• Use synchronized - A thread cannot acquire a
lock owned by another thread.– Synchronized methods• lock using an object
– Synchronized blocks• Lock using an object• Lock using a class
Synchronization…• Reentrant Synchronization - A thread can
acquire a lock that it already owns.• Atomic access– Read and write reference variable– Most primitive data type except long and double– Reads and write to all variable declared as volatile– Methods available in high level concurrency objects
Deadlock• Deadlock describes a situation where two or
more threads are blocked forever, waiting for each other.
• Guarded Blocks helps to coordinate actions to avoid deadlock
Immutable Objects• An object is considered immutable if its state
cannot change after it is constructed.– Don’t provide setter/getter and Make fields
private and final– Don’t allow sub class to overwrite method, make
class final. Use instance to create new object.– Don’t allow object to be changed or store
reference to external
High Level Concurrency Object• Lock objects: Supports locking idioms• Executors: For lunching and managing thread.• Concurrent Collections• Atomic variables• ThraedLocal Random (JDK 7)
Exercise 03: Write a simple ticketing system that can sale one
ticket at a time via 5 different sales counter
Thread Priority & Scheduler• The thread scheduler selects the highest
priority thread in the Ready-to-run and gives it CPU control
• Following are the priority levels• MIN_PRIORITY = 1• NORM_PRIORITY = 5 (default)• MAX_PRIORITY = 10
Thread Priority & Scheduler• Preemptive Scheduling:
– If a thread with a higher priority than the current running thread moves to the Ready-to-run state, then the current thread is preempted (moved to Ready-to-run state) to let the higher priority thread execute.
• Time-sliced or Round-robin Scheduling:– A running thread is allowed to execute for a fixed
length of time, after with it moves to the Read-to-run state to await its turn to run again.
High Level Concurrency Objects• Lock objects– Synchronized code relies on a simple reentrant
lock– Supports sophisticated locking– Use implicit locks like Synchronized code– Supports notify/notify all through Condition
objects
Executors• Executor Interfaces define the three executor
object types• Thread Pools are the most common kind of
executor implementation• Fork/Join is a framework (new in JDK 7) for
taking advantage of multiple processors
Executor Interfaces• Executor, a simple interface that supports
launching new tasks• ExecutorService, a subinterface of Executor,
which adds features to manage the lifecycle of individual tasks and of the executor itself
• ScheduledExecutorService, a subinterface of ExecutorService, supports future and/or periodic execution of tasks
Atomic variable• Defined in java.util.concurrent.atomic• AtomicInteger• AtomicIntegerArray• AtomicLong• AtomicLongArray etc
Exercise 04: Write a bouncing ball animation program where you can
add bouncing ball by clicking button and also have a remove ball
button