Java Programming Language Write code in Java that shows race condition using two or more threads. Give comments on first line (can be multiple lines), explain race condition that occurs in your program.
Solution:
Race condition:
Race condition in Java occurs in a multi-threaded environment when more than one thread try to access a shared resource (modify, write) at the same time. Since multiple threads try to race each other to finish executing a method thus the name race condition.
Code in Java that shows race condition using two or more threads:
class Counter implements Runnable{
private int c = 0;
public void increment() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c++;
}
public void decrement() {
c--;
}
public int getValue() {
return c;
}
// race condition that occurs in program and see how the shared variable c is giving wrong values
@Override
public void run() {
//incrementing
this.increment();
System.out.println("Value for Thread After increment "
+ Thread.currentThread().getName() + " " + this.getValue());
//decrementing
this.decrement();
System.out.println("Value for Thread at last "
+ Thread.currentThread().getName() + " " + this.getValue());
}
}
public class RaceConditionDemo{
public static void main(String[] args) {
Counter counter = new Counter();
Thread t1 = new Thread(counter, "Thread-1");
Thread t2 = new Thread(counter, "Thread-2");
Thread t3 = new Thread(counter, "Thread-3");
t1.start();
t2.start();
t3.start();
}
}
Output:
Value for Thread After increment Thread-2 3 Value for Thread at last Thread-2 2 Value for Thread After increment Thread-1 2 Value for Thread at last Thread-1 1 Value for Thread After increment Thread-3 1 Value for Thread at last Thread-3 0
It can be seen how the shared variable c is giving wrong values.
Please give thumbsup, if you like it. Thanks.
Java Programming Language Write code in Java that shows race condition using two or more threads....
Dining Philosopher problem solution- Vulnerable to starvation in c language or either java - using threads(can use semaphores but with threads) - with proper explanations and comments Please provide output of program and explanation of how to run program.
Use Java language to write this program Programming Exercise 3.20 required you to design a PID manager that allocated a unique process identifier to each process. Exercise 4.20 required you to modify your solution to Exercise 3.20 by writing a program that created a number of threads that requested and released process identifiers. Now modify your solution to Exercise 4.20 by ensuring that the data structure used to represent the availability of process identifiers is safe from race conditions. Use...
java programming language
Problem3 (1.5 points). Write a Java program to input electricity unit charges and calculate total electricity bill according to the given condition: For first 50 units Rs. 0.50/unit For next 100 units Rs. 0.75/unit For next 100 units Rs. 1.20/unit For unit above 250 Rs. 1.50/unit An additional surcharge of 20% is added to the bill Problem4 (1 points). Write a Java program to inputs weekday number and prints the weekday name using switch.
Write the code in java programming language To get some practice with recursion. You can do all this in one driver program. Write a recursive method to compute the result of the Fibonacci sequence: Fibonacci(N) = Fibonacci(N -1) + Fibonacci(N-2) N == 0 is 0 N == 1 is 1 Testing: Display the result for Fibonacci(N) and the number of function calls for N = 2, 5 and 10.
Using Java programming to answer this question. Write a method countLines(String inFilename) of a class FileUtility which reads a binary byte file with name inFilename using FileInputStream. The method returns the no. of lines containing comments starting with "//" with optional spaces (but not other characters) before it. Each line is terminated by '\n'. You may write other methods if necessary. Handle possible exception by outputting a suitable message if an exception occurs.
Synchronization, race condition, etc.
Which of the choices are correct?
Consider the code below in Java, corresponding to the implementation of a list using an array: public class MyArrayList private int[] array- new int [MAX_SIZE]; private AtomicInteger size-new AtomicInteger() public void insert(int element) do int position - size.get); array[position] - element; 3 while (!size.compareAndSet(position, position + 1)); Assume that the list is initially empty, and that two threads call insert simultaneously, one with parameter 5, and one with parameter 9....
in BASIC/BEGINNER OBJECT ORIENTATED JAVA Please write code for the following program using comments explaining the code. (You can hand write this). Give a UML example illustrating AGGREGATION. Include classes, fields, methods, instance fields. USE ONE SUPER CLASS AND TWO SUB CLASSES. Include pcode for at least one method of each class. Explain the whole part relationship and if this a deep or shallow copy.
Java programming. Introduction to Classes and Methods.
Please write the correct code, with comments so I can understand
what you did, and please make the code as SIMPLE as possible --
this is an intro to Classes/Methods practice. If your answer is
right, I will give you thumbs up rating!
Each of the following methods should be static and defined in a class called Utilities. Implement a second class calledTester that calls each of these methods
Write the code in python programming Language String Statistics: Write a program that reads a string from the user and displays the following information about the string: (a) the length of the string, (b) a histogram detailing the number of occurrences of each vowel in the string (details provided below), (c) the number of times the first character of the string occurs throughout the entire string, (d) the number of times the last character of the string occurs throughout the...
Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...