Question

JAVA Program!

Write a program that makes use of threading. You will have one thread count how many times a second has passed and the second

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// ThreadExample.java

public class ThreadExample {

      public static void main(String[] args) {

            //creating first thread

            Thread t1 = new Thread(new Runnable() {

                  //to store seconds count

                  int seconds = 0;

                  @Override

                  public void run() {

                        //loops infinitely

                        while (true) {

                              //pausing the thread for 1 second

                              try {

                                    Thread.sleep(1000);

                              } catch (InterruptedException e) {

                                    e.printStackTrace();

                              }

                              //incrementing seconds passed

                              seconds++;

                              //displaying seconds passed

                              System.out.println("seconds passed: "+seconds);

                             

                        }

                  }

            });

            //creating second thread

            Thread t2 = new Thread(new Runnable() {

                  //to store 3 seconds count

                  int seconds = 0;

                  @Override

                  public void run() {

                        //loops infinitely

                        while (true) {

                              //pausing this thread for 3 seconds

                              try {

                                    Thread.sleep(3000);

                              } catch (InterruptedException e) {

                                    e.printStackTrace();

                              }

                              //incrementing count of 3 seconds

                              seconds++;

                              //displaying three seconds passed

                              System.out.println("\t\t\tthree seconds passed: "+seconds);

                             

                        }

                  }

            });

            //starting both threads

            t1.start();

            t2.start();

      }

}

/*OUTPUT*/

seconds passed: 1

seconds passed: 2

                  three seconds passed: 1

seconds passed: 3

seconds passed: 4

seconds passed: 5

                  three seconds passed: 2

seconds passed: 6

seconds passed: 7

seconds passed: 8

                  three seconds passed: 3

seconds passed: 9

seconds passed: 10

seconds passed: 11

                  three seconds passed: 4

seconds passed: 12

seconds passed: 13

seconds passed: 14

                  three seconds passed: 5

seconds passed: 15

seconds passed: 16

seconds passed: 17

                  three seconds passed: 6

seconds passed: 18

seconds passed: 19

seconds passed: 20

                  three seconds passed: 7

seconds passed: 21

seconds passed: 22

seconds passed: 23

                  three seconds passed: 8

seconds passed: 24

seconds passed: 25

seconds passed: 26

                  three seconds passed: 9

seconds passed: 27

seconds passed: 28

seconds passed: 29

                  three seconds passed: 10

seconds passed: 30

seconds passed: 31

seconds passed: 32

                  three seconds passed: 11

seconds passed: 33

seconds passed: 34

seconds passed: 35

                  three seconds passed: 12

seconds passed: 36

seconds passed: 37

seconds passed: 38

                  three seconds passed: 13

seconds passed: 39

seconds passed: 40

seconds passed: 41

                  three seconds passed: 14

seconds passed: 42

Add a comment
Know the answer?
Add Answer to:
JAVA Program! Write a program that makes use of threading. You will have one thread count how many times a second has p...
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
  • Need some help on the following JAVA problem: Write a program in which the main thread...

    Need some help on the following JAVA problem: Write a program in which the main thread creates two threads named “Display” and “Daemon”. The first thread (Display) executes in an infinite loop and prints out its name and system time every 1 second until it is interrupted by the main thread. The second thread is a daemon thread that executes in an infinite loop and prints out its name. The main thread waits for a specific number of seconds n...

  • Dice Rollers( write java) Write a complete thread class to represent rolling dice. The thread tak...

    Dice Rollers( write java) Write a complete thread class to represent rolling dice. The thread takes a parameter that represents how many dice are being rolled at once and how many times to roll. When the thread is run, it rolls the specified number of dice the specified number of times. The thread counts for how many rolls the number on all dice match. This value is saved as instance data. Write a program to create and run several threads...

  • *USE JAVA In this project you will create program that calculate the number of times every...

    *USE JAVA In this project you will create program that calculate the number of times every word appears in a given text file. Your program will take in the name of the file as well as the number of threads to create to speed up the process. The numbers of threads must be greater than 0. The output will be saved into a file in alphabetical order. Besides creating the program itself, you are to run experiments to show how...

  • (40pts) Write a PLC program that counts how many times TWO buttons are simultaneously pressed (within 0.5 seconds o...

    (40pts) Write a PLC program that counts how many times TWO buttons are simultaneously pressed (within 0.5 seconds of each other). The system will ONLY count when the buttons are pushed at the same time (+/- 0.5 seconds). Store the count in N7:12. If the count is between 3 and 7, a red light comes on. Use the E-stop to reset the count and to set N7:12 to 0 2. (40pts) Write a PLC program that counts how many times...

  • Write a program that will allow the user to specify how many times the program will...

    Write a program that will allow the user to specify how many times the program will loop and display the title of the program include your name in the title, print from in the loop a string and count. -Assembly Language MASM 8086

  • Description: Overview: You will write a program (says wordcountfreq.c) to find out the number of words and how many times each word appears (i.e., the frequency) in multiple text files. Specifically,...

    Description: Overview: You will write a program (says wordcountfreq.c) to find out the number of words and how many times each word appears (i.e., the frequency) in multiple text files. Specifically, the program will first determine the number of files to be processed. Then, the program will createmultiple threads where each thread is responsible for one file to count the number of words appeared in the file and report the number of time each word appears in a global linked-list....

  • Write a java program that makes use of the ‘DO WHILE ’ loop. The program asks...

    Write a java program that makes use of the ‘DO WHILE ’ loop. The program asks a user to enter information about several of their friends. The information is their last name, the state they live in, their age, and their expected graduation year. After you enter one friend, write out each individually. You must use JOptionPane to input the 4 fields, and to output the 4 fields. Refer to the textbook Code Listing 4-6 to see how to use...

  • Write a program called CountFlips whose main method flips a coin 100 times and counts how...

    Write a program called CountFlips whose main method flips a coin 100 times and counts how many times each side comes up. Make sure to include comments of what is being done in the code. Verify whether the head comes up or not by calling isHeads method of Coin class. Increment the heads count if head comes up. Increment the tails count if tail comes up. Display the head and tails counts. Print the results SEND AS A TEXT FILE...

  • Java Programming: Make a Java program with two processes, a producer and a consumer. If you...

    Java Programming: Make a Java program with two processes, a producer and a consumer. If you want to use another language, clear it with me first. The producer process consists of a loop that writes the loop count (a value from 0 to 4) into a variable that it shares with the consumer process (this variable is to be initialized to 100). On each pass through the loop, before the producer writes into the shared variable, it does a random...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses 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,...

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