Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners a multithreaded program by creating a subclass of Thread
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


sfarzana
a multithreaded program by creating a subclass of Thread
0 Answer(s)      a year and 6 months ago
Posted in : Java Beginners


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]);    
}    
}    
}

Advertisement
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 jvp_9040@ymail.com 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 all the current running thread in a java program   public class RunningThreads...() + ":" + group.getClass()+"]"); int count = group.activeCount(); Thread[] threads
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
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
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
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
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
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
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
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
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
How to access Subclass?
How to access Subclass?  How to access subclass in java? please tell me the syntex with an example
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
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
thread  can parent thread be dead if child thread is not dead
Thread
Thread  what is the use of thread
Thread
Thread  Thread Life Cycle
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
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
 

 

 

DMCA.com