Programmers Files :-
1. Sync File :-https://drive.google.com/open?id=0B08RKBSts8D8aFB2cVVlVFIzTlk
*1 .ControlledThread
Ans
public class ControlledThread extends Thread {
static final int SUSP = 1;
static final int STOP = 2;
static final int RUN = 0;
private int state = RUN;
public synchronized void setState(int s) {
state = s;
if ( s == RUN ) {
notify();
}
}
public synchronized boolean checkState() {
while ( state == SUSP ) {
try {
wait();
} catch (InterruptedException e) {
// ignore
}
}
if ( state == STOP ) {
return false;
}
return true;
}
public void run() {
while ( true ) {
//doSomething();
// Be sure shared data is in consistent state in
// case the thread is waited or marked for exiting
// from run()
if ( !checkState() ) {
break;
}
}
}
}
*2. MyStack
Ans
public class MyStack {
int idx = 0;
char [] data = new char[6];
public void push(char c) {
data[idx] = c;
idx++;
}
public char pop() {
idx--;
return data[idx];
}
}
Code Files :- https://drive.google.com/open?id=0B08RKBSts8D8LVZyY3hsU25XZUE
1. Sync File :-https://drive.google.com/open?id=0B08RKBSts8D8aFB2cVVlVFIzTlk
*1 .ControlledThread
Ans
public class ControlledThread extends Thread {
static final int SUSP = 1;
static final int STOP = 2;
static final int RUN = 0;
private int state = RUN;
public synchronized void setState(int s) {
state = s;
if ( s == RUN ) {
notify();
}
}
public synchronized boolean checkState() {
while ( state == SUSP ) {
try {
wait();
} catch (InterruptedException e) {
// ignore
}
}
if ( state == STOP ) {
return false;
}
return true;
}
public void run() {
while ( true ) {
//doSomething();
// Be sure shared data is in consistent state in
// case the thread is waited or marked for exiting
// from run()
if ( !checkState() ) {
break;
}
}
}
}
*2. MyStack
Ans
public class MyStack {
int idx = 0;
char [] data = new char[6];
public void push(char c) {
data[idx] = c;
idx++;
}
public char pop() {
idx--;
return data[idx];
}
}
*3. TestMyStack
Ans
public class TestMyStack {
public static void main(String args[]) {
MyStack stack = new MyStack();
stack.push('H');
stack.push('i');
System.out.println("Stack is: " + String.copyValueOf(stack.data));
stack.pop();
stack.push('o');
System.out.println("After one pop and another push, Stack is: " +
String.copyValueOf(stack.data));
}
}
*4. TestThread
Ans
public class TestThread {
public static void main(String args[]) {
Xyz r = new Xyz();
Thread t = new Thread(r);
t.start();
}
}
class Xyz implements Runnable {
int i;
public void run() {
i = 0;
while (true) {
System.out.println("Hello " + i++);
if ( i == 50 ) {
break;
}
}
}
}
Code Files :- https://drive.google.com/open?id=0B08RKBSts8D8LVZyY3hsU25XZUE
No comments:
Post a Comment