Beruflich Dokumente
Kultur Dokumente
Rohan Pahwa
Roll No-A17
REG No-10809673
MCA(hons).
Course Code: CAP360 Course Title:Modern Programming Tools & Techniques-III
Homework 3
PART A
1. What java interface must be implements by all threads?
Ans :- Interfaces provide a way for programmers to lay the repository of
a class. They are used to design the requirements for a set of classes to
implement. The interface sets everything up, and the class or classes
that implement the interface do all the work. A class that implements an
interface must implement all methods defined in that interface. An
interface has the ability to extend from other interfaces, and (unlike
classes) can extend from multiple interfaces. Furthermore, an interface
cannot be instantiated with the new operator
Java implement by two ways of creating threads: implementing an
interface and extending a class. All tasks must implement the run()
method, whether they are a subclass of Thread or implement the
Runnable interface.
The first method of creating a thread is to simply extend from the
Thread class. Do this only if the class you need executed as a thread does
not ever need to be extended from another class. The Thread class is
defined in the package java.lang, which needs to be imported so that our
classes are aware of its definition.
import java.lang.*;
public class test extends Thread
{
public void run()
{
....
}
}
The above example creates a new class Counter that extends the Thread
class and overrides the Thread.run() method for its own
implementation. The run() method is where all the work of the Counter
class thread is done. The same class can be created by implementing
Runnable:
import java.lang.*;
public class test implements Runnable
{
Thread T;
public void run()
RUN Method {
....
}
}
package java.lang;
Thread with Runnable
public interface Runnable
interface
{
public abstract void run();
}
String name;
Thread t;
NewThread(String threadname){
name = threadname;
t.start();
try {
Thread.sleep(1000);
}catch (InterruptedException e) {
System.out.println(name + "Interrupted");
System.out.println(name + "Exiting.");
}
class DemoJoin {
try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
}catch (InterruptedException e) {
}
OutPut:-
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Two: 3
Three: 3
One: 2
Two: 2
Three: 2
One: 1
Two: 1
Three: 1
Two exiting
Three exiting
One exiting
The basic platform for multiprocessing allows for more than one computer to
be engaged in the used of the same programs at the same time. This means
that persons working at multiple work stations can access and work with data
contained within a given program. It is this level of functionality that makes it
possible for users in a work environment to effectively interact via a given
program.
Implement multiprocessing :-
PART B
Q4.What are the two methods by which we can stop threads?
Ans:- The two thread methods for stoping thread is that
A) Stop method.
B) Interrupt:-
Publicvoidinterrupt()
Interrupts this thread, causing it to continue execution if it was blocked for any reason.
System.out.println("Starting thread3...");
Thread thread3 = new GuessANumber(27);
thread3.start();
try
{
thread3.join();
}catch(InterruptedException e)
{
System.out.println("Thread interrupted.");
}
System.out.println("Starting thread4...");
Thread thread4 = new GuessANumber(75);
thread4.start();
System.out.println("main() is ending...");
}
}
Output:-
Starting hello thread...
Starting goodbye thread...
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Thread-2 guesses 27
Hello
** Correct! Thread-2 in 102 guesses.**
Hello
Starting thread4...
Hello
Hello
..........remaining result produced.
return null;
}
}
}
class SequenceInputStreamDemo {
public static void main(String args[]) throws Exception {
int c;
Vector files = new Vector();
files.addElement("/autoexec.bat");
files.addElement("/config.sys");
InputStreamEnumerator e = new InputStreamEnumerator(files);
InputStream input = new SequenceInputStream(e);
while ((c = input.read()) != -1) {
System.out.print((char) c);
}
input.close();
}
}
This example creates a Vector and then adds two filenames to it. It passes that vector of
names to the InputStreamEnumerator class, which is designed to provide a wrapper on
the vector where the elements returned are not the filenames but rather open
FileInputStreams on those names. The SequenceInputStream opens each file in turn,
and this example prints the contents of the two files.
Q6:-Write a program that will count the number of characters in a file?
Ans:-
import java.io.*;
Output:-