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



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

Have Programming Question? Ask it here!
 
 
 


sfarzana
a multithreaded program by creating a subclass of Thread
0 Answer(s)      a year and a month 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 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
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
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
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
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 access Subclass?
How to access Subclass?  How to access subclass in java? please tell me the syntex with an example
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  Thread Life Cycle
Thread
Thread  what is the use of thread
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
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
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
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   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
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
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
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
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
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
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
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
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
Exception handling in super and subclass while implementing inheritance,,?
Exception handling in super and subclass while implementing inheritance,,?  How to implement Superclass Exceptions with SubClass while implementing Inheritance concept in core java.? Please answer for this with sample code
Calling awt Frame methods from subclass
Calling awt Frame methods from subclass  Calling awt Frame methods from subclass
doubt about superclass reference and subclass object
doubt about superclass reference and subclass object  class superclass { p.bark() } class subclass extends superclass { p.bark } {p.s.v.m() superclass refvar = new subclass(); p.bark(); } Now my questions 1.Why is necessary
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
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 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
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
 

 

 

DMCA.com