Contents
- .
- ./ConcurrentThreads.java
- ./FirstThread.java
- ./index.html
- ./JabberTest.java
- ./SharedAddressSpace.java
- ./YieldJoinDemo.java
. 1/7
[top][prev][next]
./ConcurrentThreads.java 2/7
[top][prev][next]
/**
* Example that creates several concurrently
* executing threads in Java.
*/
public class ConcurrentThreads {
public static void main(String[] args) {
System.out.println("Main starting...");
Thread t1 = new CThread(1);
Thread t2 = new CThread(2);
t1.start();
t2.start();
for (int i=0; i<1000; i++) {
System.out.println("Main running");
}
System.out.println("Main ending...");
}
}
class CThread extends Thread {
private int id;
public CThread(int id) {
this.id = id;
}
public void run() {
for (int i=0; i<1000; i++) {
System.out.println("Thread " + id + " running...");
}
}
}
./FirstThread.java 3/7
[top][prev][next]
/**
* Simple example of creating a thread in Java.
*/
public class FirstThread {
public static void main(String[] args) {
System.out.println("Main thread running...");
Thread t1 = new MyThread(1);
t1.start();
System.out.println("Main thread finished...");
}
}
class MyThread extends Thread {
private int id;
public MyThread(int id) {
this.id = id;
}
public void run() {
System.out.println("Thread " + id + " running...");
}
}
./index.html 4/7
[top][prev][next]
<html>
<head><title>Examples for /home/faculty/sprenkle/public_html/cs330/examples/15-threads</title>
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/css/themes/spacelab.min.css" />
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/css/course.css" />
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/css/syntax.css" />
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/css/style.css" />
</head>
<body>
<h1>Examples for /home/faculty/sprenkle/public_html/cs330/examples/15-threads</h1>
<ul>
<li><a href=".//code.html">All IN ONE FILE (pretty syntax)</a>
<li><a href=".//ConcurrentThreads.java">ConcurrentThreads.java</a></li>
<li><a href=".//FirstThread.java">FirstThread.java</a></li>
<li><a href=".//JabberTest.java">JabberTest.java</a></li>
<li><a href=".//SharedAddressSpace.java">SharedAddressSpace.java</a></li>
<li><a href=".//YieldJoinDemo.java">YieldJoinDemo.java</a></li>
</ul>
</body>
./JabberTest.java 5/7
[top][prev][next]
/**
* From http://www.cs.usfca.edu/~parrt/course/601/lectures/threads.html
*/
class Jabber implements Runnable {
String str;
public Jabber(String s){ str = s; }
public void run() {
while (true) {
System.out.print(str);
System.out.println();
}
}
}
public class JabberTest {
public static void main(String[] args) {
Jabber jabber1 = new Jabber("1");
Jabber jabber2 = new Jabber("2");
Thread t1 = new Thread(jabber1);
Thread t2 = new Thread(jabber2);
t1.start();
t2.start();
}
}
./SharedAddressSpace.java 6/7
[top][prev][next]
/**
* Demonstration of threads shared address space.
*/
public class SharedAddressSpace {
public static void main(String[] args) {
int[] vals = {-1, -1, -1};
Thread t0 = new SASThread(0,vals);
Thread t1 = new SASThread(1,vals);
Thread t2 = new SASThread(2,vals);
t0.start();
t1.start();
t2.start();
try {
t0.join();
t1.join();
t2.join();
}
catch (InterruptedException e) {
}
for( int i=0; i < vals.length; i++ ) {
System.out.println("vals[" + i + "] = " + vals[i]);
}
}
}
class SASThread extends Thread {
private int id;
private int[] array;
public SASThread(int id, int[] array) {
this.id = id;
this.array = array;
}
public void run() {
array[id] = id;
}
}
./YieldJoinDemo.java 7/7
[top][prev][next]
/**
* Demonstration of the use of yield and join
* in Java.
*/
public class YieldJoinDemo {
public static void main(String[] args) {
System.out.println("Main starting...");
Thread t1 = new YJDThread(1);
Thread t2 = new YJDThread(2);
t1.start();
t2.start();
System.out.println("Main waiting...");
try {
t1.join();
t2.join();
}
catch (InterruptedException e) {
}
System.out.println("Main ending...");
}
}
class YJDThread extends Thread {
private int id;
public YJDThread(int id) {
this.id = id;
}
public void run() {
for (int i=0; i<100; i++) {
System.out.println("Thread " + id + " running...");
Thread.yield();
}
}
}
Generated by GNU Enscript 1.6.6.