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

Ads









Related Tutorials/Questions & Answers:
Create Thread by Extending Thread
Thread class to create thread. class ThreadClass extends 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
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
Advertisements
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
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
);       }   } }   class MyThread2 extends Thread{   MyThread2(String s){     super(s);     start.... Lets see, how to set and get the priority of a thread.   class MyThread1 extends Thread{   MyThread1(String s){     super(s);     start
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
("Thread Priority  :"+cur);       }   } }   class MyThread2 extends Thread... the priority of a thread.   class MyThread1 extends Thread{   MyThread1...());       System.out.println("Thread Priority  :"+cur);       }   } } public class ThreadPriority
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
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
Java :Thread Methods
Java :Thread Methods This section explains methods of Thread class. Thread Methods : Thread class provides many method to handle different thread...(). Example : class RunnableThread implements Runnable { Thread thread
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  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
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
Page Directive attribute - extends This tutorial  contains description of extends attribute of page Directive. extends Attribute : This attribute specifies the super class of the JSP page's implementation servlet. It allows
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
Java Thread
that a thread hold. Example : public class SimpleThread extends Thread..._PRIORITY (constant value 1). Thread class provides you 3 priority - MAX_PRIORITY...Java Thread In this tutorial we will discuss about Java Thread. Java Thread
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 priority in java
A"); } } class B extends Thread { public void run.... Thread class defines some priority constant as follows: MIN_PRIORITY = 1 NORM.... Example : A program how to set or get priority of thread in java. class
Thread Priorities
;} }   class MyThread2 extends Thread{   ... "MyThread2" and minimum priority for the first thread "... is executed only once and the second thread "MyThread2" started
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
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 Thread
Thread There are method to create thread 1)Extends the Threads Class... independently in program. Method in Object  and Thread Class... of this class is runnable 2. The Thread constructor is used to create an object
what is the difference between extends and implements
what is the difference between extends and implements  difference between extends and implements

Ads