Question

java thread question. Could you please give me a multiple thread program example that all threads...

java thread question.

Could you please give me a multiple thread program example that all threads run in parallel?

Homework Answers

Answer #1

Multithreading is a feature of OOPs which provides tasks to be executed concurrently at an intsance using each individual threads created.

Below is an example -

public class Main implements Runnable
{
       public static void main(String[] args) {
        Thread t1 = new Thread("Thread number-1");
        Thread t2 = new Thread("Thread number-2");
        t1.start();
        t2.start();
        System.out.println("Thread names are following:");
        System.out.println(t1.getName());
        System.out.println(t2.getName());
    }
    @Override
    public void run() {
    }
 
}


Exaplantion :

-- The class which contains main method should implements runnable interface as the instances of the that particular class needs to executed using threads)

-- t1 and t2 are two thread instances created to perform two different tasks simultaneously. The name of t1 instance is 'Thread number -1' and t2 instance is 'Thread number -2'

--start() is a method internally calls the run() method which is defined under runnable interface. This method used to start the execution of the that particular thread instance through which it is called. In simpler words, calling start() method means instructing compiler to execute the definition defined in run() method.

--getName() method is used to get the name of thread instantiated.

--run() contains the task related code. Here the code sequence doesn't matter after the thread starts, rather each thread starts doing their individual task mentioned in run() method.

Know the answer?
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
Similar Questions
Solve the following using java Write a program that runs three threads, each thread randomizes a...
Solve the following using java Write a program that runs three threads, each thread randomizes a number between 1 and 100. The main thread waits for all the others to finish, calculates the maximum of the numbers, which were randomized, and prints it. Random number 1: 10 Random number 2: 38 Random number 3: 81 Max: 81 Note: You should create 3 threads in adition to the main thread. Also, you can use a single thread class and create 3...
could you please give me an example of Data Collection Activity with Required Activity Component :...
could you please give me an example of Data Collection Activity with Required Activity Component : - Description of the Study - Variable of Interest - Population - Sample - Data Collection Method - Measures are taken to ensure a representative sample was collected - Raw Data, Example: For this study, the data might look as follows: 230, 543, 109, 476, 219, 72, 789, 111, 360, 967, 445, 301, 836, 281, 547, 682, 725, 370, 177, 265, 484, 638, 40,...
please give me program that solve a Proof in  Discrete write a c program
please give me program that solve a Proof in  Discrete write a c program
For this assignment you need to write a parallel program in C++ using OpenMP for vector...
For this assignment you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is: #pragma...
Question 15 The following question has multiple parts, make sure you answer all of them. Give...
Question 15 The following question has multiple parts, make sure you answer all of them. Give the definition of a confidence interval. When is it typically used? Give an example of a real-world scenario when a confidence interval might be helpful. PLEASE MAY I HAVE BRIEFLY EXPLAINED ANSWERS. I WILL APPRECIATE
I have a list of things for review in C programming, could you please give me...
I have a list of things for review in C programming, could you please give me an example and a brief explanation for each question... Thank you very much 5. Create functions that can return pointers or functions that can return structs 6. Analyze code that uses predefined strings such as  strlen, strcat, strncat, and  strtok 7. Create syntax that can perform file input and output 8. Distinguish between array and pointer notation and use both in syntax
this is for health class please give me a new example. Give an example of a...
this is for health class please give me a new example. Give an example of a measure that would benefit public health but that might be expensive to implement. •Who would benefit? •Who would pay the cost?
Create a JAVA program to display the numbers 1-10. Although you could program a solution that...
Create a JAVA program to display the numbers 1-10. Although you could program a solution that has ten lines of code like the following, think about why this would be a bad idea when developing a programming solution: Print 1 Print 2 Print 3 Print 4 (continued to Print 10) Note: Examples for Java programs (Loop and no loop) programs are in the "Important Documents" folder / Module 6.
[Java] Write a program that stores pairs of all the states and capitals (USA). The program...
[Java] Write a program that stores pairs of all the states and capitals (USA). The program should give the user a State and ask for the capital. The program should keep track of how many correct answers were given. User should see score printed at the end. IMPORTANT: Make sure the program RANDOMLY asks for the capital of a random state. This question has already been asked before in Expert Q&A but the answer never randomly gave states.
will you please give me short answers around 4 sentence... and could you please type them...
will you please give me short answers around 4 sentence... and could you please type them i do not want handwriting. If you are considering closing a warehouse and opening a new one, what people in the organization might be impacted and how might they react to the project? Select two (2) functional groups within the organization and explain why they would be impacted.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT