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
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