Question
Asked Nov 15, 2019
26 views

JAVA threads confused help

1.

A)Consider the Java program. What is the expected logical output? What is the actual output likely to be? Justify precisely your answers

B) What exactly is the purpose of the last for loop? Does it help with the issue discussed in part (a)? What would happen if we remove that loop?

C)Suppose we replace the StringBuilder in part (a) with a StringBuffer. What is the actual output now? Justify precisely your answer

public class StringBuilderTest {
public static void main(String[] args) throws InterruptedException
100
final int N
StringBuilder sb
Thread[] threads =
for (int i-0; i<N; i++)
threads[i]
new StringBuilder();
new Thread [N];
= new Thread new Runnable() {
public void run() {
for (int i-0; i<N; i++) sb.append ( "X"); }} );
for (int i-0; i<N; i++) threads [ i] . start();
for (int i-0; i<N; i++) threads [i].join();
System.out.println("String length
}}
sb.length());
=
help_outline

Image Transcriptionclose

public class StringBuilderTest { public static void main(String[] args) throws InterruptedException 100 final int N StringBuilder sb Thread[] threads = for (int i-0; i<N; i++) threads[i] new StringBuilder(); new Thread [N]; = new Thread new Runnable() { public void run() { for (int i-0; i<N; i++) sb.append ( "X"); }} ); for (int i-0; i<N; i++) threads [ i] . start(); for (int i-0; i<N; i++) threads [i].join(); System.out.println("String length }} sb.length()); =

fullscreen
check_circle

Expert Answer

star
star
star
star
star
1 Rating
Step 1

Multithreading in java: 

It is an essential feature of java that performs two or more programs to run simultaneously for the optimum use of CPU. These parts of the program are known as threads. So, threads are nothing but a process within a process. 

There are two methods to create a thread. In the given program, the way that is used to create a thread is by implementing the runnable interface.  

Below are some methods that are used in the given program:

  • run(): run() method is basically used to execute some action in the thread.
  • start(): start() method is used to create a new thread in the program.
  • join(): join() method basically waits for a thread to die as it puts the current thread in the waiting condition unless or until the thread(which called the join method) completes its job.  
Step 2

A)

According to the given program,

The number of threads created at first is 100 as the initial value of N=100.

And the append method is used to add some string to the sequence. So, methods used in the program will add 100 length string to the StringBuilder object sb.

Numbers of thread which are created in the program will execute one by one as join method is used.

So, the expected output will be:

String length = 10000

But in the given program StringBuilder class is used and this class is non-synchronized. So, multiple threads can call the methods concurrently.

It will lead to the overlapping of the threads. 

This overlapping will create chaos. Hence, there can be multiple outputs while executing the same program multiple times.

Sometimes the output will less than 10000, sometimes an exact value of 10000 and sometimes the output will be “out of memory error”.

Step 3

B)

In the last for loop join() method is used.

join() will stop all the thread expect the thread by which join() method is called for each time the for loop will execute.

In the given program StringBuilder class is used, which is non-synchronized. Hence, the...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Programing Language

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Assignment First, launch NetBeans and close any previous projects that may be open (at the top menu ...

A: A Java program that satisfies the given criteria is given below,Program://Import necessary packagesi...

question_answer

Q: Describe five components of code, indicating their role in programming. Identify the specific code t...

A: The given HTML code provide the below screenshot.

question_answer

Q: Forest and domain functional level means Windows Operating System version. which one applies? Member...

A: Explanation:A Windows Server 2008 functional level requires that all the Domain Controller (DC)s mus...

question_answer

Q: Hello! I am new to Java and have nearly finished up a project involving a mortgage table, and everyt...

A: According to the question, students need only in output spacing.To display output in proper spacing ...

question_answer

Q: what are the mantissa and exponent values if 6.75 is represented in 8-bit binary floating point repr...

A: Conversion of decimal to 8-bit floating point representation:Step 1: Convert the integer part of the...

question_answer

Q: All programming languages have a loop capability. Looping is the program logic’s ability to repeat o...

A: Program://inlcude the header files#include &lt;iostream&gt;using namespace std; //definition of main...

question_answer

Q: Using the SQL GROUP BY phrase with a SELECT statement can help detect which of thefollowing problems...

A: This question is based on SQL language . To know the right option of this question first a user must...

question_answer

Q: Think of one of your Math homework. How can you use program to reveal the answer? Write a calculatio...

A: Variables in PHP: Variables are used to store values like numbers, characters etc. that is of differ...

question_answer

Q: Shipping is free for online orders totaling $25 or more at Mississippi.com. Orders less than $25 are...

A: Note: You didn't mention any language so i use Python#get the inputs from the userunit_cost = float(...