Write a multithreaded java program where a person gains 1000 calories throughout the day. But, the person burns it off too. So, the balance is zero. That means no gain or loss in calories. Synchronization will enable you to achieve zero balance every time the program is run. Note: First, do not think about synchronization. Write the program with two threads, one java file can handle the task-I named it Zerosum. There are two functions here, gainCalories() and burnCalories(). Each thread handles one of those functions. The run() method of each thread will have the method details. You can also call those methods if you decide to develop it outside the thread block. You are keeping a count of 0 to 1000 calories for both gain and loss. You need to keep track of the balance, so that is your instance variable which you print at end of the program. At the beginning of the program balance is zero.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Write a multithreaded java program where a person gains 1000 calories throughout the
day. But, the person burns it off too. So, the balance is zero. That means no gain or loss
in calories. Synchronization will enable you to achieve zero balance every time the
program is run.
Note: First, do not think about synchronization. Write the program with two threads,
one java file can handle the task- I named it Zerosum. There are two functions here,
gainCalories() and burnCalories(). Each thread handles one of those functions. The run()
method of each thread will have the method details. You can also call those methods if
you decide to develop it outside the thread block. You are keeping a count of 0 to 1000
calories for both gain and loss. You need to keep track of the balance, so that is your
instance variable which you print at end of the program. At the beginning of the
program balance is zero.
When you run this multithreaded program without synchronization, each run will show
a result - a single integer. It can be positive integer, negative integer and occasionally 0.
It's the value of 'balance'. It's interleaved. That means both the threads are running but
not in an orderly fashion.
Then you synchronize and run the program. Each time the balance will be 0. That is the
output you want to have.
Transcribed Image Text:Write a multithreaded java program where a person gains 1000 calories throughout the day. But, the person burns it off too. So, the balance is zero. That means no gain or loss in calories. Synchronization will enable you to achieve zero balance every time the program is run. Note: First, do not think about synchronization. Write the program with two threads, one java file can handle the task- I named it Zerosum. There are two functions here, gainCalories() and burnCalories(). Each thread handles one of those functions. The run() method of each thread will have the method details. You can also call those methods if you decide to develop it outside the thread block. You are keeping a count of 0 to 1000 calories for both gain and loss. You need to keep track of the balance, so that is your instance variable which you print at end of the program. At the beginning of the program balance is zero. When you run this multithreaded program without synchronization, each run will show a result - a single integer. It can be positive integer, negative integer and occasionally 0. It's the value of 'balance'. It's interleaved. That means both the threads are running but not in an orderly fashion. Then you synchronize and run the program. Each time the balance will be 0. That is the output you want to have.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Concept of Threads
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education