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

 
 


sfarzana
a multithreaded program by creating a subclass of Thread
0 Answer(s)      2 months and 19 days 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]);    
}    
}    
}
View Answers









Related Pages:
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
Thread Creation
. 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 to follow...( ) method in the subclass from the Thread class to define the code executed
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
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
=" + 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
instead of try and catch exception handler. For instance in the above given program we
Tiny Window with subclass
program and one containing the JFrame subclass. It's perhaps more common... Java NotesExample - Tiny Window with subclass This is a reimplementation of the TinyWindow program, but using the most common style of building windows
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 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
user defined subclass of throwable class - Java Beginners
user defined subclass of throwable class  Dear Sir,pls help me to write this pgm: Program to illustrate the use of user defined subclass of throwable class  Hi Friend, Try the following: 1) public class
Parallel Processing
language. A thread is a sequential path of code execution within a program. And each thread has its own local variables, program counter and lifetime. In single... with in a program. Sometimes thread can be referred as lightweight processes because
creating multiple threads - Java Beginners
creating multiple threads  demonstrate a java program using multiple thread to create stack and perform both push and pop operation synchronously... MyThread extends Thread{ MyThread(String s){ super(s); start
Thread priority in java
. Example : A program how to set or get priority of thread in java. class..."); } } In the above program what we have done is creating two class A and B...Thread priority in java A thread is a part or entity of a  process
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 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
Thread in Nutshell
Thread in Nutshell  Hi, I m confused about what is called a thread actually in a program. There are many answer to this question on the web...; Please go through the following link: Thread Tutorials
Thread concept
Thread concept  Everytime when i run a multithread program it gives... in advance friends. Happy new year!!!!! class Newthread3 implements Runnable{ Thread t; String name; Newthread3(String threadname){ name=threadname; t=new Thread
Main Thread and Child Thread
and Child Threads used in Programming. Main thread is automatically created when program runs. Child Thread gets created by the main thread . Java Main Thread Example public class mainchild implements Runnable { Thread t1
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
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
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... of thread is controlled by Java Virtual Machine (JVM). It allows a program
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
Get Thread Name
Get Thread Name       Thread is the independent path of execution of a thread in a program... the list of thread executing in a program. GetThreadName.java
program
program  program of jdbc using ms access for creating table
Java Thread
by creating instance of a Thread. It lives in this state until you start...Java Thread In this tutorial we will discuss about Java Thread. Java Thread : A thread is light weight java program.JVM permits you to have multiple
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... independently in program. Method in Object  and Thread Class
program
program  explanation of program on extending thread class   Hi Friend, Please go through the following link: Java Threads Thanks
write following program
threaded application and multithreaded application? What are the benefits of multi... on thread synchronization with examples Q4 WAP that asks the user to input... the state transition diagram of a thread Q6 Write a java applet that displays 9
Thread Priorities
("My Thread 2");   } }   Output of the Program:  ... executing the program, the first thread is executed only once and the second... Thread Priorities     
Thread Priorities
:Thread[My Thread 1,1,main]   In this program... Thread Priorities         In Java, thread scheduler can use the thread
Thread Priorities
:Thread[My Thread 1,1,main] In this program two...;MyThread1" i.e. the after executing the program, the first thread... Thread Priorities      
Java Thread
Java Thread Tutorials In this tutorial we will learn java Threads in detail. The Java Thread class helps the programmer to develop the threaded application in Java. Thread is simple path of execution of a program. The Java Virtual Machine
How to access Subclass?
How to access Subclass?  How to access subclass in java? please tell me the syntex with an example
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
Program
Program  Create a Super class Department(dno, dname, dlocation) and a method calsalary(float basic, float DA, float HRA).Define a subclass Employee(eno, ename, dno, dname, dlocation). Calulate the salary of an employee by using
Program
Program  Create a Super class Department(dno, dname, dlocation) and a method calsalary(float basic, float DA, float HRA).Define a subclass Employee(eno, ename, dno, dname, dlocation). Calulate the salary of an employee by using

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.