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...
Java program question! If I pass null to a method, how can I use the passed...
Java program question! If I pass null to a method, how can I use the passed value to compare. Here is example code, it might be wrong, please make it could run on eclipse. Public class hw{ public static void main (String args[]) { method1(); } private static void method1() { System.out.println(method(null)); } public static String method(int[] a) { return null; } What I want to get in the output is just "()". It is not just to type the...
JAVA use the concepts in concurrent programming to write a basic multi-threaded program. You will be...
JAVA use the concepts in concurrent programming to write a basic multi-threaded program. You will be creating a program that simulates the sound of soldiers marching: “Left, Left, Left, Right, Left”. You will need one class to print “Left” and one to print “Right”, as well as a class to drive the program. LeftThread.java public class LeftThread extends Thread { public void run() { for(int i = 0; i < 10; i++) { //TODO: Print "Left" //TODO: Print "Left" //TODO:...
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,...
How to create the multiple class in Java? Please elaborate it by using the example.
How to create the multiple class in Java? Please elaborate it by using the example.
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
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?
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 an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT