Java Programming - IT Assignment Sample | Assignment Essay Help

Information Technology Assignment Sample on Java Programming

 Sample Assignments

You can download the sample Information Technology questions on Java Programming with the following question for free at the end of this page. For further assistance in Information Technology Assignment help, please check our offerings in Information Technology assignment solutions. Our subject-matter experts provide online assignment help to Information Technology students from across the world and deliver plagiarism free solution with a free Turnitin report with every solution.

(AssignmentEssayHelp does not recommend anyone to use this sample as their own work.)

Management Assignment Question

Part 1:

1. Please explain in your own words the basic concept behind each special mechanism: Locks, Monitors, Queuing, Concurrent Queues and Forks

2. Provide a good example and error free Java sample code fragment(s) to support and demonstrate the idea(s) behind each case in (1).

Management Assignment Solution on Java Programming

Original Code

The original code has been edited to make it look cleaner, but no changes have been made to the logic of the program.

Program:

import java.util.Random;

public class Findit extends Thread

{

public static void main(String[] args)

{

// generate a random number between 0 and 1000 to search

Random rand = new Random();

int number = rand.nextInt(1001);

// construct the three threads with different range

Findit[] threads = new Findit[3];

threads[0] = new Findit(number, 0, 349);

threads[1] = new Findit(number, 350, 699);

threads[2] = new Findit(number, 700, 1000);

// now start the three threads

for (int i = 0; i < threads.length; i++)

{

// set their name so they can be displayed

threads[i].setName(“Thread-” + (i + 1));

// start the threads

threads[i].start();

}

// wait until all the threads terminate

 

for (int i = 0; i < threads.length; i++)

{

try

{

threads[i].join();

}

catch (InterruptedException e)

{

 

}

 

}

 

}

private int number;

// the number to search

private int begin, end;

// the range to search

// the class constructor

// search the number in the given range [begin, end]

// it is assumed that begin <= end.

 

 

public Findit(int number, int begin, int end)

{

 

this.number = number;

this.begin = begin;

this.end = end;

}

 

@Override

public void run()

{

// use a loop to iterate the range

for (int it = begin; it <= end; it ++)

{

if (it == number)

{

// find the target number, display the thread name

String name = Thread.currentThread().getName();

                System.out.println(name +” found the target “+ number +” in the range [” + begin + “,

Read more in the complete solution PDF document at the end of this page.

Question –

“Output Window”Thread-3 found target at 771, but what about Thread-2 and Thread-1

 Explanation:

This Program basically is an example of the concept of multithreading in Java.

The objective of this program is to create a Random number between 0 to 1000

And then use 3 threads to search that number,

First thread will only look between numbers from 0 – 349

Second thread will only look between numbers from 350 – 699

Third thread will only look between numbers from 700 – 1000

And this is the reason there is only one thread showing up in the output.

If the number was suppose 360, only thread 2 will be able to search for it, because thread 1 and thread 3 will not look in this range of numbers. Only thread 2 can access the number 360 which is between range 350 – 699.

Read more in the complete solution PDF document at the end of this page.

For explanation purpose, I'll assume our random number generated was And we'll talk about Thread 1 here.

For( int it = begin  ;  it <= end ; it++)

So this loop will use the variable ' it ' with initial value equal to ' begin ' which is 0 for thread 1 and it will run till the value of it is less than or equal to ' end ' that is 349.

Now there is the ' if ' condition saying that the code inside it will only execute if the value of variable ' it ' is equal to the random number , in this case we assumed it to be 10.

When the value of variable reaches 10

If block is executed.

String name= Thread.currentThread().getName();

Which means, the name of Thread that found the value is stored in the variable 'name '

 This way if thread 2 finds the value, the variable 'name' will have the name ' Thread – 2' stored in it.

 System.out.println(name +” found the target “+ number +” in the range [” + begin + “, ” + end + “]”);

This statement will print the name of the Thread that found the value along with it's range.

 

    Download this Assignment Sample for FREE
    1. This form collects your email so that we can correspond with you through our newsletters. Checkout our Privacy policy for more information.
    2. Yes, i consent to this conditions.

     

    (Some parts of the solution has been blurred due to privacy protection policy)

    Check More Samples

    Order Now

    WhatsApp WhatsApp Us