Home Answers Viewqa Java-Beginners class MyThread2 extends Thread

 
 


monu
class MyThread2 extends Thread
0 Answer(s)      7 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

View Answers









Related Pages:
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
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  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
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
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 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
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
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
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
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 {  
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
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
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
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
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
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
JAVA THREAD - Java Beginners
this may be you got some idea package com.pack1; class TestTwo extends...: class kj implements Runnable { public static void main(String ar[])throws...); } public void fgh(int i,int p) { int sum; new Thread(public void run
Green Thread - Java Beginners
the years. This is simple program of thread public class ThreadExample extends Thread { public ThreadExample(String str) { super(str... of Green Thread in java. Thanks in advance...  Hi friend Green threads
Thread and Process - Java Beginners
, threads have their own stack space. This is thread code, public class ThreadExample extends Thread { public ThreadExample(String str) { super...Thread and Process  Dear Deepak Sir, What is the diffrence between
java thread - Java Beginners
(a); } private JPanel canvas;}class Ball extends Thread { public Ball(JPanel... BounceThreadFrame(); frame.setVisible(true); }}class BounceThreadFrame extends...Java Thread  What is thread in Java? and how can i write a Java
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
Thread in java
Thread in java  which method will defined in thread class
Java Thread Synchronization - Development process
extends Thread{ static String msg[]={"This", "is", "a", "synchronized...Java Thread Synchronization  Hi,Please help me with this coding. I... press any key on the keyboard,then one thread must stop.When I press the keys second
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
first time click on OFF button that error came class NextPage extends JFrame...(); } }); } } class LoginDemo extends JFrame { ....... LoginDemo... Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException  
thread related - Java Interview Questions
the monitor and proceed. public class WaitMethod extends Thread{ String st...(this) { notifyAll(); } }catch(Exception e){} } } class Example extends...thread related  Hi, Plz tell me how two thread will communicate
Java Thread In Applet
Java Thread Applet is a java class that runs inside the internet browser. It is used to make the gui application, network application in java Thread...;100" height="100"></applet> public class Clock extends
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
Java :Thread Synchronization
Sync extends Thread{ static String msg[]={"This", "is", "a", "synchronized...Java :Thread Synchronization This section explains how to use concept of synchronization in java Thread. Thread Synchronization : . Java supports multi
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
a multithreaded program by creating a subclass of Thread
); } } class MyThread extends Thread { static String message...: concurrent thread1: programming class ThreadSynchronization...("Interrupted!"); } } } class SynchronizedOutput
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
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 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
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 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
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
Exception in thread
Exception in thread   Hi, I have created a java file for sending a file to my mail. I am using mail.jar file. I am able to create .class file... 2 class as mentioned below SendMailTLSDFC$1.class SendMailTLSDFC.class
Event Dispatcher Thread
Runnable interface in the constructor of Thread class, a thread has been...*; import javax.swing.table.*; public class EventDispatcherExample extends...();   }   private class ColorTableModel extends AbstractTableModel {     private Color
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
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
Java :Thread getPriority Example
the priority of your thread. Example : class ThreadGetPriority implements Runnable...Java :Thread getPriority Example In this tutorial you will learn how to get thread priority in java thread. Thread getPriority() : Thread scheduler uses
Java :Thread setPriority Example
to the current thread. class ThreadSetPriority implements Runnable { Thread...Java :Thread setPriority Example In this tutorial you will learn how to set thread priority in java thread. Thread setPriority() : Thread scheduler uses
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 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
Java Thread
Java Thread       A java thread is an execution context or a lightweight process. It is a single sequential flow of control within a program. Programmer may use java thread mechanism

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.