62,614
社区成员
发帖
与我相关
我的任务
分享
package pcenshao.thread; import java.util.concurrent.Semaphore; public class SemaphoreABC { static class T extends Thread{ Semaphore me; Semaphore next; public T(String name,Semaphore me,Semaphore next){ super(name); this.me = me; this.next = next; } @Override public void run(){ for(int i = 0 ; i < 10 ; i ++){ try { me.acquire(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(this.getName()); next.release(); } } } public static void main(String[] args) { Semaphore aSem = new Semaphore(1); Semaphore bSem = new Semaphore(0); Semaphore cSem = new Semaphore(0); T a = new T("A",aSem,bSem); T b = new T("B",bSem,cSem); T c = new T("C",cSem,aSem); a.start(); b.start(); c.start(); } }
public class TestABC {
public static void main(String[] args) throws Exception {
System.out.println("start");
Thread a = new Thread(new Runner("A"));
Thread b = new Thread(new Runner("B"));
Thread c = new Thread(new Runner("C"));
a.start();
b.start();
c.start();
a.join();
b.join();
c.join();
System.out.println("finish");
}
}
class Runner implements Runnable {
private String name;
private int index;
private static int g_index = 0;
private static int g_count = 0;
private static final Object lock = new Object();
public Runner(String name) {
this.name = name;
this.index = g_count++;
}
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (lock) {
while (g_index != index) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name);
g_index = (g_index + 1) % g_count;
lock.notifyAll();
}
}
}
}
public class TestABC {
public static void main(String[] args) throws Exception {
Thread a = new Thread(new Runner("A", 0));
Thread b = new Thread(new Runner("B", 1));
Thread c = new Thread(new Runner("C", 2));
a.start();
b.start();
c.start();
a.join();
b.join();
c.join();
}
}
class Runner implements Runnable {
private String name;
private int index;
private static int g_index = 0;
private static int g_count = 0;
private static final Object lock = new Object();
public Runner(String name, int index) {
this.name = name;
this.index = index;
g_count++;
}
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (lock) {
while (g_index != index) {
try {
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(name);
g_index = (g_index + 1) % g_count;
lock.notifyAll();
}
}
}
}
import java.util.concurrent.atomic.AtomicInteger;
public class Print extends Thread{
private AtomicInteger synObj;
private int count;
private String s;
private int flag;
private int total = 0;
public Print(int count,AtomicInteger atomicInteger,String s,int flag) {
this.count = count;
this.synObj = atomicInteger;
this.s = s;
this.flag = flag;
}
public void run() {
while(true) {
synchronized(synObj) {
if(synObj.intValue()%3 == flag) {
total++;
synObj.set(synObj.intValue()+1);
System.out.println(s);
synObj.notifyAll();
if(total == count) {
break;
}
}else {
try{
synObj.wait();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
public static void main(String[]args) throws Exception {
AtomicInteger synObj = new AtomicInteger(0);
Print a = new Print(10,synObj,"A",0);
Print b = new Print(10,synObj,"B",1);
Print c = new Print(10,synObj,"C",2);
a.start();
b.start();
c.start();
}
}
public class TestABC {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
Thread a = new Thread(new Runner("A"));
a.start();
a.join();
Thread b = new Thread(new Runner("B"));
b.start();
b.join();
Thread c = new Thread(new Runner("C"));
c.start();
c.join();
System.out.println();
}
}
}
class Runner implements Runnable {
private String name;
public Runner(String name) {
this.name = name;
}
public void run() {
System.out.println(name);
}
}