Write a multithreaded Java code which generates 4 threads for writing the latest
ID: 3772367 • Letter: W
Question
Write a multithreaded Java code which generates 4 threads for writing the latest temperature of 4 cities on a shared file. The java code should allow one thread at one time to write the temperature value on the shared file. As a result, the temperature of only one city should appear on the file at one time. Note that the temperature value should be generated using rand() method. The system should not allow the same thread to write on the file in two consequent times, and it should allow other thread to use the file for writing.Explanation / Answer
import java.io.*;
public class threadtemp{
public static void main(String ar[]){
Thread t=new Thread();
for(int i=0;i<4;i++){
int rand=new Random().randInt(100);
t.run();
System.out.println("Temperature is:"+);
}
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.