Part 1: Thread creation (30%)a. Using Java multithreading library, write a Java program that calculates the sum of the numbersfrom 1 to 100,000,000. Split the numbers between four threads equally where each threadcalculates the sum of one fourth of the numbers. For example, the 1st thread will calculate the sumof the numbers from 1 to 25,000,000 whereas the 2nd thread will calculate the sum of the numbersfrom 25,000,001 to 50,000,000 and so forth. The main thread will have to print out the sum aftergathering the results. Note that you have the choice to create threads by either implementingRunnable interface or extending Thread class.b. Now, write a sequential version of the program described above using a single main thread (i.e.,without multithreading). Make sure to record and print out the time spent during the execution ofboth sequential and multithreaded versions (hint: you may consider usingSystem.currentTimeMillis() to record execution time).Part 2: Synchronization (40%)Assume that we have a file named “sharable.txt” which can be shared among several threads. Wewant to write a program that controls the access to that file in a way that only one thread at a timeis allowed to access it (i.e., for writing/appending purposes). Using Java multithreading, write aprogram that creates three threads and assigns a number to each thread. Then, each thread will startrunning by executing a code for opening the file “sharable.txt” and writing the following lines:Thread x started writingThread x is currently writingThread x finished writing – Student NameYour program should allow only one thread -at any given time-to access the file and write in it. Italso should keep away any thread from overwriting the lines written by any other threads (hint:use synchronized methods/blocks). Finally, when the execution of your program is completed, theoutput stored in “sharable.txt” should look like the following – put your name in place “YourName”:Thread x started writingThread x is currently writingThread x finished writing – Your NameThread y started writingThread y is currently writingThread y finished writing – Your NameThread z started writingThread z is currently writingThread z finished writing – Your NamePart 3: Interrupt handling (30%)In Java multithreading environment, one thread can send an interrupt to another by calling theinterrupt() method on the Thread object for the target thread (i.e., the thread to beinterrupted). To handle interrupts in a target thread, Java allows two approaches. One is performedby writing an exception handler for InterruptedException (only applicable if the targetthread is invoking methods which throw that exception such as sleep). The other approach isperformed by periodically checking the interrupt status flag Thread.interrupted andperforming the handling routine when that flag is set to true.Write a Java program that illustrates the use of the two approaches described above. Your programshould start by creating two threads, each thread should use a different interrupt handling approach.Then, the program needs to send interrupts to each one of the created threads such that a threadneeds to return (i.e., stop execution) after receiving an interrupt from the main thread. Make sureto output (print out) the status of each thread before and after being interrupted.Submission:1. The output from each program i.e. three parts (you may use screenshots).2. The source code for each program in (.java) format.
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here