class MyThread2 extends Thread

class MyThread2 extends Thread

Hi Friend,

Try the following code:

import java.io.*;
import java.util.*;
class MyThread1 extends Thread {
private PipedReader pr;

private PipedWriter pw;

MyThread1(PipedReader pr, PipedWriter pw) {
this.pr = pr;
this.pw = pw;
}

public void run() {
try {
int i;
for (i=1;i<10;i++)
{
int j;
for (j=2; j<i; j++)
{
int n = i%j;
if (n==0)
{
break;
}
}
if(i == j)
{
pw.write(i+"\n");
} 
}

pw.close();



} catch (IOException e) {
}
}
}

class MyThread2 extends Thread {
private PipedReader pr;
private PipedWriter pw;

MyThread2(PipedReader pr, PipedWriter pw) {
this.pr = pr;
this.pw = pw;
}
public void run() {
try {
int f1, f2 = 1, f3 = 1;
for (int i = 1; i <10; i++) {
pw.write(f3+"\n");
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
pw.close();
} catch (IOException e) {
}
}
}
class MultithreadedProgram {
public static void main(String[] args) throws Exception {
ArrayList list1=new ArrayList();
ArrayList list2=new ArrayList();
PipedWriter pw1 = new PipedWriter();
PipedReader pr1 = new PipedReader(pw1);
MyThread1 mt1 = new MyThread1(pr1, pw1);
System.out.println("Prime Numbers: ");
mt1.start();
int item1;
while ((item1 = pr1.read()) != -1){
char ch1=((char) item1); 
System.out.print(Character.toString(ch1));
list1.add(Character.toString(ch1));
}
pr1.close();
PipedWriter pw2 = new PipedWriter();
PipedReader pr2 = new PipedReader(pw2);
MyThread2 mt2 = new MyThread2(pr2, pw2);
System.out.println("Fibonacci Numbers: ");
mt2.start();
int item2;
while ((item2 = pr2.read()) != -1){
char ch2=((char) item2); 
System.out.print(Character.toString(ch2));
list2.add(Character.toString(ch2));
}
pr2.close();
System.out.println("Elements common to both lists are:");
list1.retainAll(list2);
for(int i=0;i<list1.size();i++){
System.out.print(list1.get(i));
}

}
}

Thanks

Post Answer

View Answers









Related Tutorials/Questions & Answers:
Difference between extends thread class vs implements runnable interface - Java Interview Questions
Difference between extends thread class vs implements runnable interface  Hi Friends, can you give difference between extending thread class... want to extend the Thread class then it will make your class unable to extend
implements runnable n extends thread - Java Beginners
implements runnable n extends thread  what is the difference between implements runnable n extends thread? public class...(); class StringThreadImplement implements Runnable { private String } }  Hi public class
Advertisements
Java Thread class
It is created by extending the Thread class or implementing Runnable interface Java Thread Class Example public class thread1 extends Thread { @Override... Java Thread Class is a piece of the program execution Java has
thread class - Java Beginners
the following code: class Incrementor extends Thread{ int cnt1 = 0; boolean...; } } class Decrementor extends Thread{ int cnt2 = 100; int cnt1...thread class  Create 2 Thread classes.One Thread is Incrementor
Java Sleep Thread
or GUI programming for animation Java Sleep Thread Example public class sleep1 extends Thread { public sleep1(String s) { super(s); } @Override...("mythread2"); s1.start(); s2.start(); } } Output Thread
Create Thread by Extending Thread
are extending Thread class to create thread. class ThreadClass extends... 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
Why the wait(),notify() should not avaialable in Thread class.
Why the wait(),notify() should not avaialable in Thread class.  wait(),notify(),notifyall() is available in Object class.We are using those method in Threading then why don't they put the 3 methods in thread class
The extends keyword
. For example, class B want to inherits the features of class A using extends.... class A { public int a=5;; } class B extends A { public... code you can see that class B extends class A means it inherits the feature
The extends Keyword
. It specifies the superclass in a class declaration using extends keyword... of java.lang.Object.  For example, Class X extends class Y to add functionality, either... of the 'extends' keyword.  public class
Thread
Thread  class Extender extends Thread { Extender(Runnable run... :"); //new Thread(new Implementer()).start(); } } class Implementer... Implementer Thread is started: "); } } public class ThreadDemo { public static
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 Priorities
to set and get the priority of a thread.   class MyThread1 extends Thread...());       System.out.println("Thread Priority  :"+cur);       }   } }   class MyThread2 extends Thread{   MyThread2(String s){     super(s);     start();   } public
Thread
Thread  will this code work..? class A extends Thread { public... in your code. Here is your modified code: class A extends Thread { public... some code like this: class A extends Thread { public void run
Thread Priorities
  :"+cur);       }   } }   class MyThread2 extends Thread{   MyThread2(String s..._TO_REPLACE_4   class MyThread1 extends Thread{   MyThread1(String s...");     MyThread2 m2=new MyThread2("My Thread 2");   } }   Output
Thread
methods of thread class.   Java Create Thread There are two main ways of creating a thread. The first is to extend the Thread class and the second... the Thread class and the second is to implement the Runnable interface. Please
Diff between Runnable Interface and Thread class while using threads
Diff between Runnable Interface and Thread class while using threads  Diff between Runnable Interface and Thread class while using threads   Hi Friend, Difference: 1)If you want to extend the Thread class
Diff between Runnable Interface and Thread class while using threads
Diff between Runnable Interface and Thread class while using threads  Diff between Runnable Interface and Thread class while using threads   Hi Friend, Difference: 1)If you want to extend the Thread class
Java :Thread Methods
Java :Thread Methods This section explains methods of Thread class. Thread Methods : Thread class provides many method to handle different thread... : class RunnableThread implements Runnable { Thread thread; public
Class Loader
Class Loader       The Java ClassLoader is a an abstract class which extends the Object class. Java class loader is a part of the Java Runtime Environment that dynamically loads Java
Thread
.   Java Thread Example class ThreadExample{ static int...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
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  What is multi-threading? Explain different states of a thread... processor system. States of Thread: New state ? After the creations of Thread instance the thread is in this state but before the start() method invocation
Thread
Thread  what happen when we call the Wait(),Notify() and NotifyAll() methods in the Thread
Thread in java
Thread in java  which method will defined in thread class
Extends Attribute of page Directive
Extends Attribute of page Directive   How use language extends... of the Super class of the Java class used by the JSP engine for the translated Servlet. Syntax : ADS_TO_REPLACE_1 <%@ page extends = "package.class"%> <
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
Page Directive attribute - extends
want another super class in place of HttpJspBase then use extends.   <...Page Directive attribute - extends This tutorial  contains description of extends attribute of page Directive. extends Attribute : This attribute
about implements and extends - Java Beginners
about implements and extends  hello, class A extends B implements c // this is valid statement class A implements c extends B // this is invalid... A extends B implements C but class A implements C extends B is invalid plz explain
Object Class Methods in Java
We are going to discus about Object Class Methods in Java. The java.lang.Object class is the root of the class hierarchy tree in JDE(java development environment). Every Java class extends the java.lang.Object class directly
ModuleNotFoundError: No module named 'extends'
ModuleNotFoundError: No module named 'extends'  Hi, My Python... 'extends' How to remove the ModuleNotFoundError: No module named 'extends... to install padas library. You can install extends python with following command
ModuleNotFoundError: No module named 'extends'
ModuleNotFoundError: No module named 'extends'  Hi, My Python... 'extends' How to remove the ModuleNotFoundError: No module named 'extends... to install padas library. You can install extends python with following command
ModuleNotFoundError: No module named 'extends'
ModuleNotFoundError: No module named 'extends'  Hi, My Python... 'extends' How to remove the ModuleNotFoundError: No module named 'extends... to install padas library. You can install extends python with following command
Thread Priorities
; class MyThread2 extends Thread{   MyThread2... "MyThread2" and minimum priority for the first thread "... is executed only once and the second thread "MyThread2" started
Java Thread
_TO_REPLACE_4 public class SimpleThread extends Thread { private int c = 3... (constant value 10) to MIN_PRIORITY (constant value 1). Thread class provides...Java Thread In this tutorial we will discuss about Java Thread. Java Thread
Extending thread - Java Beginners
. For example : class SimpleThread extends Thread { public SimpleThread...Extending thread  what is a thread & give me the programm of exeucte the thread   Hi friend, Thread : A thread is a lightweight
what is the difference between extends and implements
what is the difference between extends and implements  difference between extends and implements
what is the difference between extends and implements
what is the difference between extends and implements  difference between extends and implements
Java Exception Thread
; and Thread Class Object...)Extends the Threads Class( java.lang.thread)ADS_TO_REPLACE_3 2)Implement Runnable interface( java .lang. thread) Understand Exception in Threads. 1.A class name
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
Java AclNotFoundException Class Hierarchy Diagram
In this section we will discuss about the class hierarchy of AclNotFoundException Class in Java. This class extends the java.lang.Exception class... Control List) which has no existence. Hierarchy of this class is shown
Java Exception Class Hierarchy Diagram
extends the Throwable class. All errors and exception classes are the subclasses of java.lang.Throwable which further extends the java.lang.Object class... class. The class Exception and its subclasses are defined in such a way
class
class  Is a class a subclass of itself
Java Daemon Thread
can be set as daemon thread. Java Daemon Thread Example public class deamon extends Thread { public deamon(String s) { super(s); } @Override... Daemon thread is the supporting thread. It runs in the background
Thread priority in java
of thread in java. class A extends Thread { public void run...); } System.out.println("Exit from Thread A"); } } class B extends Thread.... Thread class defines some priority constant as follows:ADS_TO_REPLACE_1 MIN
J2ME Form Class
J2ME Form Class       In this J2ME Extends Form example, we are going to discuss about form, extending form in our class and appending values to it. But first of all lets
What Is Thread In Java?
Example using extending Thread class MyThread.java class MyThread extends... Thread can be used by two ways either by extending a Thread class...[]){ MyThread m1=new MyThread("Thread Example Using Extending Thread Class
Thread concept
in advance friends. Happy new year!!!!! class Newthread3 implements Runnable{ Thread t; String name; Newthread3(String threadname){ name=threadname; t=new Thread...Thread concept  Everytime when i run a multithread program it gives
Thread Life Cycle Example in java
public class A extends Thread { public void run() { System.out.println("...;); } } B.java public class B extends Thread { public void run... of Thread class or you can create a subclass of Thread and then you can create

Ads