Question

Type, Compile, Run all syntactically complete or mostly complete example code .You may need to change...

Type, Compile, Run all syntactically complete or mostly complete example code .You may need to change some syntax to make them compile and run

import java.io.*; import java.lang.*; import java.util.*;

class MyThreadExampleMutex{
public static void main(String[] args) {

new HelloThread (1). start ();
new HelloThread (2). start ();
new HelloThread (3). start (); System.out.print("Global.buffer Content = "); for (int i = 0; i < 9; i++) {

System.out.print(Global.buffer[i] + "; ");

}} }

class Global {
public static int[] buffer = new int[15]; public static int index = 0;

}
class HelloThread extends Thread {

int threadID; HelloThread(int ID) {

this.threadID = ID; }

public void run() {
for (int i = 0; i < 3; i++) {

Global.buffer[Global.index] = this.threadID;

Global.index++; }

} // run
} // end Thread

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class MyThreadExampleMutex {

public static void main(String[] args) {

new HelloThread(1).start();

new HelloThread(2).start();

new HelloThread(3).start();

System.out.print("Global.buffer Content = ");

for (int i = 0; i < 9; i++) {

System.out.print(Global.buffer[i] + "; ");

}

}

}

class Global {

public static int[] buffer = new int[15];

public static int index = 0;

}

class HelloThread extends Thread {

int threadID;

HelloThread(int ID) {

this.threadID = ID;

}

public void run() {

for (int i = 0; i < 3; i++) {

Global.buffer[Global.index] = this.threadID;

Global.index++;

}

} // run

} // end Thread

21 つつ B Console X terminated> MyThreadExampleMutex Java Application] C:Program FilesJavaljre 1.8.0 14binljavaw.exe (Oct 25, 2018 Global.buffer Content 0; 3; 3; 2; 2; 2; 1; 1; 1;

Add a comment
Know the answer?
Add Answer to:
Type, Compile, Run all syntactically complete or mostly complete example code .You may need to change...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both...

    Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both the “ReadInput” and the “Count” classes. Currently these classes extends Thread. You should now use the Runnable interface to implement the thread. The output of the program should not change. I have also attached the class ThreadType2 to give you a working example on how to implement a thread using the Runnable interfacethe program is here package threadExamples; import java.util.Scanner; public class InteractiveCounting {...

  • Please write where its written write your code here!! please use java for the code! please...

    Please write where its written write your code here!! please use java for the code! please use the given code and I will rate thanks Magic index in an array a[1..n] is defined to be an index such that a[ i ] = i. Given an array of integers, write a recursive method to find the first magic index from left to right. If one exists in the given array, return the index number i, otherwise return -1. Here are...

  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • Please explain if the following code is actually correct. If the following code correct, please explain...

    Please explain if the following code is actually correct. If the following code correct, please explain why the code works and is also correct. Don’t use * Java’s Integer .toBinaryString(int) in this program./* According to the textbook and the instructor, I am not supposed to use arrays such as int binary[] = new int[25]; I guess this is the reason why this problem is starting to look kind of hard.   Chapter 5 Exercise 37: Java Programming * * (Decimal to...

  • THIS IS MY CODE ALL I NEED IS THE SECTIONS TO BE FILLED IN AND CODE...

    THIS IS MY CODE ALL I NEED IS THE SECTIONS TO BE FILLED IN AND CODE SHOULD RUN. FILL IN THE CODE PLEASE. package p02; import static java.lang.System.*; import java.io.*; import java.util.*; public class P02 {    public static Vector<Double> wallet=new Vector<Double>();    public static void addMoneyToWallet(){    }    public static void removeMoneyFromWallet(){    }    public static void displayMoneyInWallet(){    }    public static void emptyWallet(){    }    public static void main(String[] args){ int choice=0; String menu="Wallet...

  • in java please fix code between 14 and 20 to make the code compile Write a...

    in java please fix code between 14 and 20 to make the code compile Write a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10). The array's size may differ from 4. Display ari ay Values 1 pas [ pa: 6 public void displayValues(int [] arrayVals) { 7 int i; 8 9 for (i = 0; i < arrayVals.length; ++i) { 19 System.out.print(arrayVals[i] +...

  • Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args)...

    Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args) {         TestThread thread = new TestThread();     }     @Override     public void run() {         printMyName();     }     private void printMyName() {         System.out.println("Thread is running");     } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...

  • Can anyone help me with this code? I've never worked with insertion sort and I'm having...

    Can anyone help me with this code? I've never worked with insertion sort and I'm having trouble. I posted the instructions and the included code. Thank you! Insertion Sort Given an array of integers, sort the array in ascending/descending order by using Insertion sort. Reference: http://www.algolist.net/Algorithms/Sorting/Insertion_sort Input 4368 92157 Where, • First line represents the type of ordering. • Second line represents the size of the array. • Third line represents the array elements. Output 123456789 + Test Case(s) DriverMain.java...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT