first commit
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import java.util.*;
|
||||
|
||||
class ThreadExample extends Thread {
|
||||
String s;
|
||||
Random r;
|
||||
public ThreadExample(String s) {
|
||||
this.s = s;
|
||||
this.r = new Random();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
for(int i=0; i<5; i++) {
|
||||
System.out.println(s);
|
||||
try {
|
||||
this.sleep(r.nextInt(100));
|
||||
} catch (InterruptedException e)
|
||||
{}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
new ThreadExample("String One").start();
|
||||
new ThreadExample("String Two").start();
|
||||
}
|
||||
}
|
||||
60
CS2335/lab4/Lab_files/Examples/Threading/SimpleThread.java
Normal file
60
CS2335/lab4/Lab_files/Examples/Threading/SimpleThread.java
Normal file
@@ -0,0 +1,60 @@
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class SimpleThread implements Runnable {
|
||||
|
||||
private boolean terminate;
|
||||
|
||||
public SimpleThread() {
|
||||
// super("Sleepy Thread");
|
||||
|
||||
terminate = true;
|
||||
}
|
||||
|
||||
public boolean isAlive() {
|
||||
return !terminate;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
terminate = false;
|
||||
|
||||
while(!terminate) {
|
||||
System.out.println("SimpleThread is alive!");
|
||||
// try {
|
||||
// this.sleep(2000);
|
||||
// } catch(InterruptedException ie) {
|
||||
// System.out.println("SimpleThread's sleep was interrupted!");
|
||||
// break;
|
||||
// }
|
||||
for(int i=1; i<2000000; i+=2) {
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("\nSimpleThread is dead...");
|
||||
}
|
||||
|
||||
public void kill() {
|
||||
terminate = true;
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
SimpleThread st = new SimpleThread();
|
||||
new Thread(st).start();
|
||||
|
||||
String input = null;
|
||||
BufferedReader stdIn=new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
try {
|
||||
while((input = stdIn.readLine()) != null) {
|
||||
if(input.equals("Kill")) {
|
||||
st.kill();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch(IOException ioe) {
|
||||
System.err.println("Caught IO Exception: " + ioe.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}// end of class SimpleThread
|
||||
25
CS2335/lab4/Lab_files/Examples/Threading/ThreadExample.java
Normal file
25
CS2335/lab4/Lab_files/Examples/Threading/ThreadExample.java
Normal file
@@ -0,0 +1,25 @@
|
||||
import java.util.*;
|
||||
|
||||
class ThreadExample extends Thread {
|
||||
String s;
|
||||
Random r;
|
||||
public ThreadExample(String s) {
|
||||
this.s = s;
|
||||
this.r = new Random();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
for(int i=0; i<5; i++) {
|
||||
System.out.println(s);
|
||||
try {
|
||||
this.sleep(r.nextInt(100));
|
||||
} catch (InterruptedException e)
|
||||
{}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
new ThreadExample("String One").start();
|
||||
new ThreadExample("String Two").start();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user