Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.

Similar presentations


Presentation on theme: "Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012."— Presentation transcript:

1 Practical OOP using Java Training @ Basis Faqueer Tanvir Ahmed, 08 Jan 2012

2 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

3 What is a computer? InputOutput Process

4 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

5 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

6 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

7 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.

8 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

9 Exercise 01: Write your first multi- threaded Java application.

10 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

11 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

12 Exercise 02: Write sample program to demonstrate sleep and join

13 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.

14 Synchronization – Memory Consistency Errors int counter = 0; // Shared variable counter++;// incremented by thread A System.out.println(counter); // sometime printed by thread B

15 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

16 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

17 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

18 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

19 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

20 High Level Concurrency Object Lock objects: Supports locking idioms Executors: For lunching and managing thread. Concurrent Collections Atomic variables ThraedLocal Random (JDK 7)

21 Exercise 03: Write a simple ticketing system that can sale one ticket at a time via 5 different sales counter

22 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

23 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.

24 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

25 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

26 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

27 Atomic variable Defined in java.util.concurrent.atomic AtomicInteger AtomicIntegerArray AtomicLong AtomicLongArray etc

28 Exercise 04: Write a bouncing ball animation program where you can add bouncing ball by clicking button and also have a remove ball button

29 Exercise 05: Simple animation program by changing image


Download ppt "Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012."

Similar presentations


Ads by Google