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



Home Answers Viewqa Java-Beginners class MyThread2 extends Thread
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


monu
class MyThread2 extends Thread
0 Answer(s)      2 years and 5 months ago
Posted in : Java Beginners


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


Advertisement
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
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 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
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... int number; }  class B extends A {  
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
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
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
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
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
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
("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
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
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
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
thread
thread  can parent thread be dead if child thread is not dead
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  what is the use of thread
Thread
Thread  Thread Life Cycle
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
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
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 : <%@ page extends = "package.class"%> <%@page import
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
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
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
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
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
 

 

 

DMCA.com