Question

Develop a multithreaded application in C# that creates 4 separate threads. Associate each thread to one...

Develop a multithreaded application in C# that creates 4 separate threads. Associate each thread to one distinct method. Each method should contain time-consuming calculations (if you wish, you may use for loops to, to make the execution time even longer.) You may wish to get the input from the user and pass them to the 4 methods that would run in 4 threads you created. The idea is to let all the methods run concurrently. Please make sure that your methods do some useful calculations based on the input entered by the users. Use your creativity for this matter.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot for code is attached for reference

Program Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp5
{
class Program
{
public static void Main(string[] args)
{
//taking user input
Console.WriteLine("Enter an Integer: ");
//converting input string to integer
int n = Convert.ToInt32(Console.ReadLine());

//creating 4 thread and invoking distict method and passing 2 parameters
Thread t1 = new Thread(() => doSomething(n,"Thread1"));
Thread t2 = new Thread(() => doSomething(n, "Thread2"));
Thread t3 = new Thread(() => doSomething(n, "Thread3"));
Thread t4 = new Thread(() => doSomething(n, "Thread4"));
//thread starts when it see this command
t1.Start();
t2.Start();
t3.Start();
t4.Start();

//simple message to mention we are inside main thread
Console.WriteLine("Inside Main Program Last Statement");

}

//Distict method which all 4 threads call to print value of sum
static void doSomething(int n,string str)
{
int sum = 0;
for (int i = 1; i <= 10; i++)
{
//simply we are adding value of i with sum
sum = i + n;
Console.WriteLine("In " + str+ " Value of i is: "+sum);
//if value of sum reaches 7 thread stops or abort
if (sum == 7)
Thread.CurrentThread.Abort(); //we are aborting current thread if criteria met

}

}
}
}

Output

Add a comment
Know the answer?
Add Answer to:
Develop a multithreaded application in C# that creates 4 separate threads. Associate each thread to one...
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
  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • 1. The first task in this assignment creates the pid manager whose implementation can simply be...

    1. The first task in this assignment creates the pid manager whose implementation can simply be a single class. Of course, you can create any other classes you might need to implement the pid manager. You may use any data structure of your choice to represent the availability of process identifiers. One strategy adopts Linux’s approach of a bitmap in which a value of 0 at position i indicates that a process id of value i is available and a...

  • C Programming - Please Help us! Implementing Load Balancing, the 3 Base Code files are at the bot...

    C Programming - Please Help us! Implementing Load Balancing, the 3 Base Code files are at the bottom: Implementing Load Balancing Summary: In this homework, you will be implementing the main muti-threaded logic for doing batch based server load balancing using mutexes Background In this assignment you will write a batch-based load balancer. Consider a server which handles data proces- sing based on user requests. In general, a server has only a fixed set of hardware resources that it can...

  • CO 4) In terms of data types and their uses, which one of the following is...

    CO 4) In terms of data types and their uses, which one of the following is NOT correct? The TEXT data type can be used for Last Name. The Date/Time data type can be used for 10/31/2012. The Currency data type can be used for Account balance. The AutoNumber data type can be used for Customer Name. Flag this Question Question 125 pts (CO 5) All of the following describe form usage except a form user should have less input...

  • Write a C program for: One technique for dealing with deadlock is called “detect and recover.” In...

    Write a C program for: One technique for dealing with deadlock is called “detect and recover.” In this scheme, some procedure is used to identify when a deadlock occurs, and then another procedure is used to deal with the blocked processes. One technique to identify a deadlock is to maintain a resource graph that identifies all processes, all resources, and the relationships between them (that is, which processes exclusively own which resources, and which processes are blocked waiting for which...

  • C PROGRAM, A FOPEN FILE NEED TO BE CREATED Objective To review reading from a file....

    C PROGRAM, A FOPEN FILE NEED TO BE CREATED Objective To review reading from a file. To use records (an instance of a struct) To use Dynamic Memory Allocation To create and use a dynamically allocated array of structs To use enumerated types in a useful manner (more info given on Discussion board!) The Problem Bad economic times has forced the UCF administration to think outside the box for alternative methods of income. Specifically, we now have a UCF lottery,...

  • please use python and provide run result, thank you! click on pic to make it bigger...

    please use python and provide run result, thank you! click on pic to make it bigger For this assignment you will have to investigate the use of the Python random library's random generator function, random.randrange(stop), randrange produces a random integer in the range of 0 to stop-1. You will need to import random at the top of your program. You can find this in the text or using the online resources given in the lectures A Slot Machine Simulation Understand...

  • The following are screen grabs of the provided files Thanks so much for your help, and have a n...

    The following are screen grabs of the provided files Thanks so much for your help, and have a nice day! My Java Programming Teacher Gave me this for practice before the exam, butI can't get it to work, and I need a working version to discuss with my teacher ASAP, and I would like to sleep at some point before the exam. Please Help TEST QUESTION 5: Tamagotchi For this question, you will write a number of classes that you...

  • 1. Private data fields: name (of String type, initialized to “Bob”), endurance (of int type, initialized to 4) and happiness (of int type, initialized to 3);

    Language: JAVAPart a: Write a class called Geniegotchi with:1. Private data fields: name (of String type, initialized to “Bob”), endurance (of int type, initialized to 4) and happiness (of int type, initialized to 3);2. Public methods:-void setName(String newName) : renames Genie with newName, printsnewName confirmation to screen;void setName(String newName) : renames Genie with newName, printsnewName confirmation to screen;- intgetEndurance() : returns current endurance;-intgetHappiness() : returns current happiness;-void feed() : this method increases current endurance by 1 if endurance is less...

  • Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin...

    Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin at the University of Washington, Seattle, who originally wrote this assignment (for their CSE 142, in Java) This program focuses on classes and objects. Turn in two files named Birthday.cs and Date.cs. You will also need the support file Date.dll; it is contained in the starter project for this assignment. The assignment has two parts: a client program that uses Date objects, and a...

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