Sample solution of java assignment help on thread life cycle is building concepts of multi-threading, object locking advanced concepts of Java programming.
This assignment has been given to the students in order to understand the concepts of java programming and some basic advance programming features. The tasks provides knowledge about multi-threading and its various features and methods which will guide to develop an application based on advance programming of Java.
Define the various thread states and thread transition
Thread is a smallest unit of processing in terms of java. It provides a distinct path to execute the program. Threads share the common area of memory and if the exception occurs in one threads then it effected threads don’t affect the other threads. [Also Read: – Procedural Programming OOPS Assignment Help]
Various states of thread class:
- New: This is the states of thread class, which instance has been created but not started yet or the start () method has not been appealed on the thread.
- Run-able: When the system resource has been created by the ‘start ()’ method. Then the method schedules and calls the threads to run. This state of thread is called run-able state.
- Timed-waiting: The state of thread in which the thread is waiting for the identified waiting time. The thread is in time-waiting states by calling some methods with an identified waiting time. In the time-waiting state the threads calling the following methods:
- ‘Object.wait’with timeout
- ‘Thread.join’ with timeout
- park Until
- park Nanos
- Waiting: If a thread calling any of the methods given below without the timeout then the threat is in waiting state. The waiting thread is waiting for another threat to perform or terminate the specific task.
- wait() without timeout
- join() without timeout
- park() without timeout
- Blocked: This is the type of thread state in which the thread is in blocked state while its waiting for a monitor lock to enter or reenter the synchronized method after calling object.wait().
- Terminated: When the execution of program is finished then the threat becomes in terminated state.
Figure 1 various states of thread class
Thread transition is a process that shows changes made in the different stage of program execution. The actions taking by the operating system called the transition. [Also Read: – Enhanced Entity Relationship Database Assignment Help]
Figure 2 thread transition
Evaluate the necessity and the process of object locking
Object locking is a tool which is used to synchronize a non-static code block or method so that the only one threat can able to execute the code block on given the class instance.
It is necessary to make the instance level data thread safe. In java a single thread allow to lock the same objects various times. All monitors region linked with the object reference in the java programming.
Object locking is occurs when any thread in the JVM (java virtual machine) request a lock whenever it comes at the beginning of a different type of monitor region. When any thread class comes under the first instruction in the monitor region then the thread must obtain a lock on the referenced object. Until the thread not obtains the lock, it doesn’t allow the code to be executed. Once the lock is obtained, the block of protected code will be entered by the thread class. The lock on the associated object is released when the threads leaves the block. [Also Read: – Educational App Development Android Assignment Help]
Define thread starvation and thread deadlock and how wait, notify and notify All methods can be used to avoid
When a thread is constantly denied access to resources and as result the thread is unable to progress then the thread starvation occurs. It generally occurs when the shared resources are consumed for long time periods by the greedy threads. In case of extended period time, the threads not getting enough CPU time to process enough progress.
There is some common cause of thread starvation:
- The higher priority threads consume all the CPU time from lower priority threads.
- The threads are blocked indeterminately waiting to enter a synchronized block, because other threads are constantly allowed the access before it.
In java, Deadlock is a programming situation in which many threads are blocked constantly and waiting for each other. The deadlock generally arises with the multiple threads and resources when the multiple threads need the same locks but obtain them in different order. It is a part of multi-threading. [Also Read: – ERD Database Management System Assignment]
Figure 3 example of thread deadlock
In java three methods are used in object class which allows the threads to communicate about the lock status of the resource. These methods are:
- Wait (): Wait() is a method used in object class. It consist of three variance, the one which waits indefinitely for another threads to call notify. All or notify method in the object to wake up the current waiting thread. The other two variance puts the current thread in wait for definite time period before the other thread will wake up.
- Notify(): Notify() is the method which wake up the specific or only one thread among all the threads present in the object class for execution. The operating system thread management help the notify () methods to choose the thread.
- Notify all(): The notify All() methods is the type of method which wakes up all the threads waiting in the object class for execution. The execution priority of threads to execute first depends on the implementation of the operating system.
These methods are used to avoid thread starvation and thread deadlock but it depends on the design. The deadlock and starvation can easily solved by using the wait(), notify() and notify All() nearby the code. [Also Read: – Staff Payroll System Database Assignment Help]
Demonstrate the use of both java.lang.Thread and java.lang.Runnable
Java.lang.thread is a class in java. It is a thread of execution in a java program. The basic threading abstraction is implemented by the java.lang.thread class. All the thread in java is created and controlled by the unique object of the java.lang.thread class. We can build the thread class by extending the java.lang.thread.
The java.lang.runnable interface can create a thread in java by passing a class that implements the interface. It also favors composition over the inheritance and more flexible with others. The java.lang.runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The used class is intended to be executed by a thread and define a method of no arguments called the code run. The runnable interface provides a general protocol for objects that wish to execute code while they are active. The java.lang.runnable is designed to provide a common protocol for the objects which wish to execute the code while the objects are active. The objects of a thread class represent the threads in java and the implementing threads is archived in two ways:
- Extending the java.lang.threads class.
- Implementing the java.lang.runnable interface.
Critically evaluates the limitations of non-generic Collections API and how to refactor code to use the generic versions
When any element is taken out of the Collection, then we must cast it to the type of element which is stored in the collection. This method is unsafe besides being it inconvenient. The compiler of the system does not check that either your cast is same as the collection’s type, the cast can be fail at the run time.
The Generic collection provides a way to communicate the type of a collection to the compiler, so it can be checked. When the compiler knows the element or object type of the collection, then compiler can check the element which we have used the collection reliably and can insert the correct casts on values that being taken out of the collection.
Figure 4 without generic
Figure 5 same example modified to use generic
When we use the code<type>, read it as “of type”, the announcement above reads as “collection of string c” The code using the generic is safer and clearer. We eliminate the number of extra parenthesis and unsafe cast. The compiler of the system verified at compile time which the type of constraints are not disrupted at run time. The reason behind it the programs automatic compile without the warnings. So we can state that with the certainty it will not throw a Class Cast Exception at the run time of the program. The net effect of the generics is improved readability especially in the large programs. [Also Read: – Understand object oriented design concepts]
Following is the example of using raw (non-parameterized) collection with the generics (parameterized) collections, which shows the limitation.
Figure 6 non-generic collection
Difference between correct and incorrect overrides of hash Code and equals methods
Hash Code method
The hash Code of any objects is an integer which is like its address stored in the memory. Its maps to a memory address of all the objects. The hash Code of any object can be retrieved by using the hash Code() method.
For example let’s take the object is bike.
Bike bikeA = new bike();
Int code = bikeA.hashCode(); (result is the hash Code of bike).
Overrides of hash Code method
The hash Code and equals method should be reliable with each other. When we evaluate the two objects in the class with the help of equal method, then both the object must give the same integer hash Code. We have to return 0 (zero) by the hash Code method for all the objects present the class.
All the objects in java inherit a method which is called equals method. The equals method compares the two object value returned by the hash Code method with each other, if the compared object is equal to each other then it returns the true else it returns false. The two objects are equal if both the objects are sharing the same location in the memory with each other.
Bike bikeA = new bike(“tvs, Appache”);
Bike bikeB = bikeA; (returns the value true)
Boolean is Equal = bikeA.equals(bikeB);
Overrides of equal method
The equal method can be overwritten. To overwrite the equal methods we have to compare the objects with each other. So if we compare two bikes then we compare the price, company and model of the bike. If the compared value is false then we can overwrite the equal method by comparing the other objects.
In this assignment the main focus is to let developer acquainted with the advance programming features. It helps in building strong concepts of multi-threading, object locking and other advanced concepts of Java programming.
”Coursera – Free Online Courses From Top Universities”, Coursera, 2016. [Online]. Available: https://www.coursera.org/learn/java-programming. [Accessed: 15- Apr- 2016].
”Features of Java | Core Java Tutorial | Studytonight”, Studytonight.com, 2016. [Online]. Available: http://www.studytonight.com/java/features-of-java.php. [Accessed: 15- Apr- 2016].
Top Grade Assignment provide technical assignment writing service based on case study requirements in affordable prices and we are providing most flexible online assignment writing help, so book your Assignment with us, order now