Thread 2. Singleton Pattern 3. Strategy Pattern 4.: Sleep
Thread 2. Singleton Pattern 3. Strategy Pattern 4.: Sleep
Thread
2. Singleton Pattern
3. Strategy Pattern
4.
Process:
Process has a self-contained execution environment. Each process has its own memory space.
Thread:
Light weight processes.
Thread exists within a process. Every process has a at least one thread.
Thread’s share the process’s resources, including memory and open files.
https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html
2.Subclass thread
Sleep
Thread.sleep causes the current thread to suspend execution for specific period.
This is an efficient way of making the processor time available to threads in the application or to other applications.
Two overloaded versions of sleep are provided.
Joins
The join method allows the one thread to wait for the completion of another.
If a t is thread object whose thread is currently executing t.join() causes current thread to pause execution
Overloads of join allow the programmer to specify a waiting period. However, as with sleep, join is dependent on the OS for
timing, so you should not assume that join will wait exactly as long as you specify.
Interrupts
An interrupt is an indication to the thread that it should stop what it is doing and do something else.
A thread sends interrupt by invoking interrupt on thread object for the thread to be interrupted. For the interrupt mechanism
to work properly, the interrupted thread must support its own interruption.
When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. The non-
static isInterrupted method, which is used by one thread to query the interrupt status of another, does not change the
Synchronization
Threads communicate directly by sharing access to fields and the objects reference fields refer to.
1.Thread interference
2.Memory Consistency
Thread Contention
It occurs when two or more threads try to access the same resource simultaneously and cause the java runtime to execute one
Interference happens when two operations, running in different threads, but acting on the same data, interleave. This means
that the two operations consist of multiple steps, and the sequences of steps overlap.
Memory Consistency
Memory consistency errors occur when different threads have inconsistent views of what should be the same data .
Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object
is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods. (An
important exception: final fields, which cannot be modified after the object is constructed, can be safely read through non-
synchronized methods, once the object is constructed) This strategy is effective, but can present problems with liveness,
https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
Constructors cannot be synchronized. Using a synchronized keyword with a constructor is a syntax error.
https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html
Executors allow us to separate thread creation and management from rest of the application
https://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html
Executor Interfaces
1.Executor
2.Executor Service
Constructors
The purpose of java constructor is to initialize the newly created object before it is used.
Sometimes there is a need of initializing an object in different ways. This can be done using constructor
overloading. Different constructors can do different work by implementing different line of codes and
According to the situation , a constructor is called with specific number of parameters among overloaded constructors.
Singleton Pattern
It ensures a class has only one instance and provides global point of access to it.
/*
*/
/*
* at every call.
*/
if (_instance == null) {
return _instance;
/*
Adapter Pattern
Converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t
otherwise because of incompatible interfaces.
Blocking Queue:
It is a FIRST IN FIRST OUT data structure which blocks or times out when we try to add an element
Factory pattern:
Observer Pattern:
It defines one to many dependency between objects so that when one object changes the state , all of
its dependents are notified and updated automatically.
Sorting Algorithms
Linear Time:
An algorithm is said to take linear time, or O(n) time, if its time complexity is O(n). Informally, this
means that the running time increases at most linearly with the size of the input. More precisely, this
means that there is a constant c such that the running time is at most cn for every input of size n.
For example, a procedure that adds up all elements of a list requires time proportional to the length
of the list, if the adding time is constant, or, at least, bounded by a constant.
Comparison Sorts
1.Bubble Sort
2.Merge Sort
Bubble Sort
Bubble Sort is an elementary sorting algorithm. It works by repeatedly exchanging adjacent elements, if necessary.
is_sorted = true;
System.out.println("\n");
Selection Sort
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering
ascending order) from unsorted part and putting it at the beginning.
// Java program for implementation of Selection Sort
class SelectionSort
int n = arr.length;
int min_idx = i;
min_idx = j;
// element
arr[min_idx] = arr[i];
arr[i] = temp;
int n = arr.length;
System.out.print(arr[i]+" ");
System.out.println();
}
// Driver code to test above
ob.sort(arr);
System.out.println("Sorted array");
ob.printArray(arr);