1 Homework 4 1. (10 pts) For the following program, explain the interesting elements related to threads. Focus on explaining the output of the

1

Homework 4

1. (10 pts) For the following program, explain the interesting elements related to threads. Focus on
explaining the output of the program.

1 public class TaskThreadDemo {
2 public static void main (String args []) {
3 String [] sa = {“a”, “X”, “+”, “.”};
4 for (String s: sa) {
5 Runnable ps = new PrintChar (s, 200);
6 Thread ts = new Thread (ps, s);
7 ts.start ();
8 } // end for each character
9 } // end main
10 } // end class TaskThreadDemo
11
12 class PrintChar implements Runnable {
13 String ch;
14 int times;
15
16 public PrintChar (String c, int n) {
17 ch = c;
18 times = n;
19 } // end constructor
20
21 public void run () {
22 for (int i = 0; i < times; i++) {
23 System.out.print (ch);
24 } // end for loop
25 } // end method run
26 } // end class PrintChar

2. (10 pts) What is changed if the method called on line 7, start(), is replaced with run()? Explain (of
course). Focus on explaining the output of the program.

3. (10 pts) What is changed if the method Thread.yield() is added between lines 23 and 24? Explain.
Focus on explaining the output of the program.

4. (10 pts) Modify the above program so that the Thread.sleep method is called after each character has
been printed causing it to sleep for 500 milliseconds. Describe how that modification has altered the
output and explain why the change had the effect that you described.

5. (10 pts) Modify the above program so that the Thread.sleep method is called after each thread is
created in the main method causing it to sleep for 500 milliseconds. Describe how that modification has
altered the output and explain why the change had the effect that you described.

2

Grading Rubric:

Attribute Meets Does not meet
Problem 1 10 points

Explains the interesting elements
related to threads. Focuses on
explaining the output of the program.

0 points
Does not explain the interesting elements
related to threads. Does not focus on
explaining the output of the program.

Problem 2 10 points
Explains what is changed if the
method called on line 7, start(), is
replaced with run().Focuses on
explaining the output of the program.

0 points
Does not explain what is changed if the
method called on line 7, start(), is replaced
with run(). Does not focus on explaining
the output of the program.

Problem 3 10 points
Explains what is changed if the
method Thread.yield() is added
between lines 23 and 24. Focuses on
explaining the output of the program.

0 points
Does not explain what is changed if the
method Thread.yield() is added between
lines 23 and 24. Does not focus on
explaining the output of the program.

Problem 4 10 points
Explains how the output is changed if
the Thread.sleep method is called
after each character has been
printed.

0 points
Does not explain how the output is
changed if the Thread.sleep method is
called after each character has been
printed.

Problem 5 10 points
Explains how the output is changed if
the Thread.sleep method is called
after each thread is created in the
main method.

0 points
Does not explain how the output is
changed if the Thread.sleep method is
called after each thread is created in the
main method.

Share This Post

Email
WhatsApp
Facebook
Twitter
LinkedIn
Pinterest
Reddit

Order a Similar Paper and get 15% Discount on your First Order

Related Questions

Understanding Operating Systems (Zero-cost course material) Unit 5 Lab

Understanding Operating Systems (Zero-cost course material) Unit 5 Lab Assignment 5: Networking and Security (40 points) In this assignment, you will learn to check network configurations and services and to configure an IP address on a computer. For each of the two questions, do the following: 1. Create a document (use

Research – System Analysis and design Assignment 9.1 – Business Processes and Process Management Research a scholarly paper or

Research – System Analysis and design Assignment 9.1 – Business Processes and Process Management Research a scholarly paper or professional video on  “Business Processes, and Process Specifications”  and reflect on only one (1) of the following topics: · “Processes”: What type of business system’s Processes exist? · “Specifications”: How important

1. Prepare a professional cover letter Articulate effective interview strategies Explain the difference between a “good” interview and a “bad” interview

1. Prepare a professional cover letter Articulate effective interview strategies Explain the difference between a “good” interview and a “bad” interview Lecture notes There are 3 sections of a cover letter that are required when writing a professional cover letter to include with your resume: Introductory paragraph (to attract the

PROJECT DESCRIPTION The use of digital media has transformed how companies communicate with their customers. The use of the websites, YouTube, e-books, e-mail

PROJECT DESCRIPTION The use of digital media has transformed how companies communicate with their customers. The use of the websites, YouTube, e-books, e-mail and various forms of social media such as Facebook, Twitter, Pinterest, Snapchat, Instagram, and blogs has shaped current day communication strategies. Project 1 Consists of: 1. Project

CSCI 351 Assignment 2 60 points Instruction: · Show your own work (

CSCI 351 Assignment 2 60 points Instruction: · Show your own work (at least 50% penalty otherwise) · Submit a single WORD document (*.doc or *docx only) containing all your answers to the assignment folder (“Assignment X”) under D2L (at least 10% penalty otherwise) · Make sure you submit the