1. Given the following code: public class Test ( static String message; private static class Worker...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
1. Given the following code: public class Test ( static String message; private static class Worker extends Thread { public void run() ( try ( sleep (1000); } catch (InterruptedException e) {} message "I work."; public static void main(String args[]) throws InterruptedException { (new Worker()).start(); message = "I do not work."; Thread.sleep(2000); System.out.println(message); } a) [3 pts] The above program should print out "I work." Is it guaranteed to always do this? If not, why not? b) [3 pts] Would it help to change the parameters of the two invocations of Sleep? c) [4 pts] How would you guarantee that all changes to message will be visible to the main thread? 1. Given the following code: public class Test ( static String message; private static class Worker extends Thread { public void run() ( try ( sleep (1000); } catch (InterruptedException e) {} message "I work."; public static void main(String args[]) throws InterruptedException { (new Worker()).start(); message = "I do not work."; Thread.sleep(2000); System.out.println(message); } a) [3 pts] The above program should print out "I work." Is it guaranteed to always do this? If not, why not? b) [3 pts] Would it help to change the parameters of the two invocations of Sleep? c) [4 pts] How would you guarantee that all changes to message will be visible to the main thread?
Expert Answer:
Answer rating: 100% (QA)
a The above program may not always print out I work This is because the message variable is shared between the two threads and there is no synchroniza... View the full answer
Related Book For
Introduction To Java Programming And Data Structures Comprehensive Version
ISBN: 9780136520238
12th Edition
Authors: Y. Daniel Liang
Posted Date:
Students also viewed these computer network questions
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
Managing Scope Changes Case Study Scope changes on a project can occur regardless of how well the project is planned or executed. Scope changes can be the result of something that was omitted during...
-
Give the decoding key for the encoding keys in Problems 2536. Minus 4, divide by 2.
-
Which draws more current, a 100-W lightbulb or a 75-W bulb? Which has the higher resistance?
-
To determine whether the pipe welds in a nuclear power plant meet specifications, a random sample of welds is selected and tests are conducted on each weld in the sample. Weld strength is measured as...
-
On February 20, 2009, Cedar Valley Aviation, a wholly owned subsidiary of Aerial Services, Inc. (ASI), brought a Piper 522AS (Cheyenne II) in for maintenance to Des Moines Flying Service, Inc....
-
Southwood Furniture Company is a U.S.-based furniture manufacturer that offshored all of its actual manufacturing operations to China about a decade ago. It set up a distribution center in Hong Kong...
-
Household income in marketing surveys is often reported in brackets: for example, under $15,000, $15,000$25,000, ... , over $95,000. (a) Suppose that household income is the outcome variable in a...
-
Is the mean or the median more likely to provide a better measure of typical sales revenue for companies included in the Fortune 500?
-
A 250-kg beam 2.1 m in length slides broadside down the ice with a speed of 21 m/s. A 66-kg man at rest grabs one end as it goes past and hangs on as both he and the beam go spinning down the ice....
-
Kellogg Company (K) produces, markets, and distributes cereal and food products including Cheez-It, Coco Pops, Rice Krispies, and Pringles. The following partial income statements (in millions) were...
-
Read about David Ogilvy, also known as the "Father of Advertising".Discuss his qualities and skills and how you can use them in your career as a marketing or public relations professional....
-
Given the currently cyber landscape, what resonates with you regarding the US' vulnerability across the world when it comes to our cyberinfrastructure?
-
If the companies advertise an expenditure is 400,000 what up addicted sales give the answer in dollars
-
describe how identity management could be an effective tool in the workplace. Provide several examples.
-
In what way might the MARR be adjusted? Group of answer choices a. Higher if the risk is low b. Higher if the risk is high c. Lower if the risk is high d. None of above
-
The cost curve for the city water supply is C(Q) = 16 + 1/4 Q2, where Q is the amount of water supplied and C(Q) is the cost of providing Q acre-feet of water. (An acre-foot is the amount of water...
-
Write the bin2Dec(String binaryString) method to convert a binary string into a decimal number. Implement the bin- 2Dec method to throw a NumberFormatException if the string is not a binary string....
-
Listing 28.14, NineTail.java, presents a solution for the nine tails problem. Revise this program for the 4 * 4 16 tails problem. Note it is possible that a solution may not exist for a starting...
-
Rewrite Listing 38.11 to handle a large factorial. Use the BigInteger class introduced in 14.12. Data from Listing 38.11 package chapter38: 2 3 1mport java.text.: 4 5 publ 1c class NewFactorialBean {...
-
Given your response to item 2, how would these measures be useful? What could they be used for? Turnover is costly for organizations. In addition to the direct costs of recruiting, hiring, and...
-
How does New Belgium introduce fun into the workplace? How does having fun keep employees engaged and enthusiastic about their work and the company? Kim Jordan and Jeff Lebesch, the husband and wife...
-
Visit the New Belgium Brewing Web site (www.newbelgium.com) to learn more about the company. The company has a strong culture that values environmental sustainability. Here are its core beliefs: We...
Study smarter with the SolutionInn App