a multithreaded program by creating a subclass of Thread

a multithreaded program by creating a subclass of Thread

I want The threads will execute the following output in the suitable interface.

like this OUTPUT


thread1: Java
thread1: is
thread2: Java
thread1: an
thread2: is
thread1: exciting
thread2: an
thread1: new
thread2: exciting
thread1: language
thread1: for
thread1: concurrent
thread2: new
thread1: programming.
thread2: language
thread2: for
thread2: concurrent
thread2: programming.

but mine not display like that output why ?

My output :


thread1: Java
thread2: Java
thread2: is
thread1: is
thread2: an
thread1: an
thread2: exciting
thread1: exciting
thread2: new
thread2: language
thread2: for
thread2: concurrent
thread1: new
thread2: programming
thread1: language
thread1: for
thread1: concurrent
thread1: programming

class ThreadSynchronization    
{    
    public static void main(String args[])    
    {    
        MyThread thread1 = new MyThread("thread1: ");    
        MyThread thread2 = new MyThread("thread2: ");    
        thread1.start();    
        thread2.start();    
        boolean thread1IsAlive = true;    
        boolean thread2IsAlive = true;    
        do {    
           if (thread1IsAlive && !thread1.isAlive()) {    
               thread1IsAlive = false;    

           }    
           if (thread2IsAlive && !thread2.isAlive()) {    
               thread2IsAlive = false;    

           }    
        } while(thread1IsAlive || thread2IsAlive);    
    }    
}    

class MyThread extends Thread    
{    
static String message[] =    
{ "Java", "is", "an", "exciting", "new", "language", "for", "concurrent", "programming"};    

    public MyThread(String id)    
    {    
        super(id);    
    }    

    public void run()    
    {    
        SynchronizedOutput.displayList(getName(),message);    
    }    

    void randomWait()    
    {    
        try {    
           sleep((long)(1100*Math.random()));    
        } catch (InterruptedException x) {    
           System.out.println("Interrupted!");    
        }    
    }    
}    

class SynchronizedOutput    
{    
public static void displayList(String name,String list[])    
{    
for(int i=0;i<list.length;++i) {    
MyThread t = (MyThread) Thread.currentThread();    
t.randomWait();    
System.out.println(name+list[i]);    
}    
}    
}
View Answers









Related Tutorials/Questions & Answers:
a multithreaded program by creating a subclass of Thread
a multithreaded program by creating a subclass of Thread  I want The threads will execute the following output in the suitable interface. like...); } } class MyThread extends Thread { static String message
multithreaded program help!!!!
multithreaded program help!!!!  Write a multithreaded program that calculates various statistical values for a list of numbers. This program... separate worker threads. One thread will determine the average of the numbers
Advertisements
creating trafficsignal using thread
creating trafficsignal using thread  please give me code how to create trafic signal using thread and give me it to my email [email protected] thanks
thread program for calculator implementation
thread program for calculator implementation  Hi i'm prem i need calculator progrm in java that are implemented by Thread interface.....pls strong text
java thread program
java thread program  write a java program to find out the current running thread in a java program   public class RunningThreads{ public...() + ":" + group.getClass()+"]"); int count = group.activeCount(); Thread[] threads
java thread program
java thread program  write a java program to find out all the current running thread in a java program   public class RunningThreads...() + ":" + group.getClass()+"]"); int count = group.activeCount(); Thread[] threads
Visual Studio Program creating
Visual Studio Program creating  I'am using Visual Studio 2012 rightnow. And I'am trying to write a program. What it can do is input our email username and Password in the provided bar and the select the email website from
Creating multiple Threads
to override run() method.Example : Code for creating multiple thread. public...In this section you will learn how to create multiple thread in java. Thread is a path of execution within a program, it is a part of process. JVM allow
sleep method in thread java program
sleep method in thread java program  How can we use sleep method in thread ?   public class test { public static void main(String...) { } } } } Output Delhi Bihar Goa Pune mumbai Description:- In this thread
Create a multi-thread program that calcualtes values for a list of numbers
Create a multi-thread program that calcualtes values for a list of numbers  Write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed a series of numbers
thread is a sequential path of code execution within a program.
thread is a sequential path of code execution within a program.   thread is a sequential path of code execution within a program. what is the meaning if sequential path of code execution? please explain me
thread is a sequential path of code execution within a program.
thread is a sequential path of code execution within a program.   thread is a sequential path of code execution within a program. what is the meaning if sequential path of code execution? please explain me
thread is a sequential path of code execution within a program.
thread is a sequential path of code execution within a program.   thread is a sequential path of code execution within a program. what is the meaning if sequential path of code execution? please explain me
thread is a sequential path of code execution within a program.
thread is a sequential path of code execution within a program.   thread is a sequential path of code execution within a program. what is the meaning if sequential path of code execution? please explain me
thread is a sequential path of code execution within a program.
thread is a sequential path of code execution within a program.   thread is a sequential path of code execution within a program. what is the meaning if sequential path of code execution? please explain me
thread is a sequential path of code execution within a program.
thread is a sequential path of code execution within a program.   thread is a sequential path of code execution within a program. what is the meaning if sequential path of code execution? please explain me
thread is a sequential path of code execution within a program.
thread is a sequential path of code execution within a program.   thread is a sequential path of code execution within a program. what is the meaning if sequential path of code execution? please explain me
thread is a sequential path of code execution within a program.
thread is a sequential path of code execution within a program.   thread is a sequential path of code execution within a program. what is the meaning if sequential path of code execution? please explain me
thread is a sequential path of code execution within a program.
thread is a sequential path of code execution within a program.   thread is a sequential path of code execution within a program. what is the meaning if sequential path of code execution? please explain me
ModuleNotFoundError: No module named 'multithreaded_chicken'
ModuleNotFoundError: No module named 'multithreaded_chicken'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'multithreaded_chicken' How to remove the ModuleNotFoundError
ModuleNotFoundError: No module named 'multithreaded_chicken'
ModuleNotFoundError: No module named 'multithreaded_chicken'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'multithreaded_chicken' How to remove the ModuleNotFoundError
ModuleNotFoundError: No module named 'multithreaded_chicken'
ModuleNotFoundError: No module named 'multithreaded_chicken'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'multithreaded_chicken' How to remove the ModuleNotFoundError
Thread
Thread  Explain two ways of creating thread in java. Explain at three... ways of creating a thread. The first is to extend the Thread class and the second... Create Thread There are two main ways of creating a thread. The first is to extend
need help creating a lift program - Java Beginners
need help creating a lift program  Classes, Methods, Constructors please i need help to create an elevator program Simulating an Elevator write an Elevator class containing various methods. Then, write a program
write a program for creating html page with images and 2 buttons
write a program for creating html page with images and 2 buttons  write a program for creating html page with images and 2 buttons
Thread
Thread  What is multi-threading? Explain different states of a thread... a program or a process to execute many tasks concurrently (at the same time... processor system. States of Thread: New state ? After the creations of Thread
Get Current Thread
; A Thread is the independent path of execution in a program. Many thread runs concurrently with a program. Some threads are Multithread, This thread refers to two or more thread running simultaneously within a program
Thread
=" + i); } public static void main(string args[]) { A a = new A(); Thread t = new thread(a); t.start(); } } Is it possible to run above program with out...Thread  will this code work..? class A extends Thread { public
how to control program/thread 's access authority - Security
how to control program/thread 's access authority£¿  how to control program/thread 's access authority like program can't change I/O and change...(){ String s = "This is thread " + ThreadCount++; Vector vec = new
thread
thread  can parent thread be dead if child thread is not dead
Thread
Thread  Thread Life Cycle
Thread
Thread  what is the use of thread
Thread
Thread  Write a Java program to create three theads. Each thread should produce the sum of 1 to 10, 11 to 20 and 21to 30 respectively. Main thread....   Java Thread Example class ThreadExample{ static int
Thread Creation
_TO_REPLACE_1 I. Extending the java.lang.Thread Class For creating a thread a class have to extend the Thread Class. For creating a thread by this procedure you have... the run( ) method in the subclass from the Thread class to define the code
ModuleNotFoundError: No module named 'subclass-register'
ModuleNotFoundError: No module named 'subclass-register'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'subclass-register' How to remove the ModuleNotFoundError: No module
ModuleNotFoundError: No module named 'subclass-register'
ModuleNotFoundError: No module named 'subclass-register'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'subclass-register' How to remove the ModuleNotFoundError: No module
ModuleNotFoundError: No module named 'init_subclass'
ModuleNotFoundError: No module named 'init_subclass'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'init_subclass' How to remove the ModuleNotFoundError: No module named
Thread
to the thread constructor eventhough we had created only one thread and if you say we have added to point to the current thread then why we have not added this in the following line "s=s1" Pls reply...... class MyThread extends Thread { Thread
How to access Subclass?
How to access Subclass?  How to access subclass in java? please tell me the syntex with an example
Thread
Thread  what happen when we call the Wait(),Notify() and NotifyAll() methods in the Thread
Thread
Thread  class Extender extends Thread { Extender(Runnable run...(); } public void run(){ System.out.println("Extender Thread is Started :"); //new Thread(new Implementer()).start(); } } class Implementer
Java Exception Thread
the program. Many Thread run concurrently in the program. Multithread are those.... Thread in a program is imported from java.lang.thread class.InMultithread..._TO_REPLACE_2 5)Thread can run independently in program. Method in Object 
Get Thread Name
Get Thread Name       Thread is the independent path of execution of a thread in a program. The JVM provides an  application to execute the multiple thread running
Thread
Thread   there are two threads running at a time.. when am updating a values in database. both thread halt and stop for moment till it get updated into database... so i dnt want thread to get halts for tht moment of period. whats
Lock while inserting/updating database in multithreaded.
Lock while inserting/updating database in multithreaded.  Hi, I am having a multithreaded process which replicates the data from many table to one... 1month, I am facing one issue which is like: One of my thread get struct while
Thread
instead of try and catch exception handler. For instance in the above given program we
Program
Program  a program to prove that default constructor of super class is available to subclass by default. Define a parameterize constructor for super... in subclass
program
program  program of jdbc using ms access for creating table
Create Thread by Extending Thread
Create Thread by Extending Thread This section explain how to create thread by extending Thread class in java. Extending Thread : You can create thread by extending Thread class and then by creating instance of that class you can
Multithreading in Java
concurrently. A multithreaded program has two or more parts running simultaneously. Each part is called a thread and has a separate path of execution. A thread is run inside a process, which consists of the memory space allocated

Ads