public class Ex10_1_CaseThread extends Thread{
String studentName;
public Ex10_1_CaseThread(String studentName) {
System.out.println(studentName+"申请访问服务器");
this.studentName=studentName;
}
public void run() {
for(int i=0;i<5;i++) {
System.out.println("当前的服务对象是"+studentName+"同学");
try {
Thread.sleep((int)(Math.random()*2000));
}
catch(InterruptedException ex) {
System.err.println(ex.toString());
}
}
}
public static void main(String[]args) {
Ex10_1_CaseThread t1=new Ex10_1_CaseThread("张三");
Ex10_1_CaseThread t2=new Ex10_1_CaseThread("李四");
t1.start();
t2.start();
}
}
public class Ex10_2_SynchronizedCase {
CardAccount cardaccount;
Thread messConsume,schoolingConsume;
Thread mobileConsume,libraryConsume,marketConsume;
Ex10_2_SynchronizedCase(){
cardaccount=new CardAccount();
messConsume=new Thread(cardaccount);
schoolingConsume= new Thread(cardaccount);
mobileConsume= new Thread(cardaccount);
libraryConsume= new Thread(cardaccount);
marketConsume= new Thread(cardaccount);
messConsume.setName("messConsume");
schoolingConsume.setName("schoolingConsume");
mobileConsume.setName("mobileConsume");
marketConsume.setName("marketConsume");
libraryConsume.setName("libraryConsume");
}
public static void main(String[]args) {
Ex10_2_SynchronizedCase ex =new Ex10_2_SynchronizedCase();
ex.caserun();
}
public void caserun() {
try {
messConsume.start();
libraryConsume.start();
marketConsume.start();
mobileConsume.start();
schoolingConsume.start();
}catch(Exception e){
System.err.println(e.toString());
}
}
}
class CardAccount implements Runnable{
int fund=1000;
String accessType;
public void setFund(int num) {
fund=num;
}
public synchronized void accountAccess(int num,boolean k) {
for(int i=0;i<3;i++) {
accessType=Thread.currentThread().getName();
if(k)
fund+=num;
else {
fund-=num;
}
try {
System.out.println("当前线程是"+accessType+",账户剩余资金为"+fund+"。");
Thread.sleep(2000);
}catch(InterruptedException ex) {
System.err.println(ex.toString());
}
}
}
public void run() {
accessType=Thread.currentThread().getName();
if(accessType.equals("messConsume"))
accountAccess(5,false);
else if(accessType.equals("schoolingConsume"))
accountAccess(100,false);
else if(accessType.equals("mobileConsume"))
accountAccess(20,false);
else if(accessType.equals("libraryConsume"))
accountAccess(2,false);
else if(accessType.equals("marketConsume"))
accountAccess(6,false);
}
}
public class Ex10_3_deadlockCase {
public static void main(String[]args) {
DeadlockRisk dead=new DeadlockRisk();
ClassThread ct1=new ClassThread(dead);
ClassThread ct2=new ClassThread(dead);
ClassThread ct3=new ClassThread(dead);
ClassThread ct4=new ClassThread(dead);
ct1.start();
ct2.start();
ct3.start();
ct4.start();
}
}
class ClassThread extends Thread{
private DeadlockRisk dead;
ClassThread(DeadlockRisk dead){
this.dead=dead;
}
public void run() {
int k=(int)(10*Math.random());
if(k>5) {
this.dead.projectorBorrow();
}else {
this.dead.roomOrder();
}
}
}
class DeadlockRisk{
private static class ClassRoom{
public boolean value;
}
private static class Projector{
public boolean value;
}
private ClassRoom room=new ClassRoom();
private Projector pr1=new Projector();
public void roomOrder() {
synchronized(room) {
room.value=true;
System.out.println(Thread.currentThread().getName()+"预定了教室");
try {
Thread.sleep(1000);
}catch(InterruptedException ex) {
System.err.println(ex.toString());
}
synchronized(pr1) {
pr1.value=true;
System.out.println(Thread.currentThread().getName()+"借到了投影仪");
}
}
}
public void projectorBorrow() {
synchronized(pr1) {
pr1.value=true;
System.out.println(Thread.currentThread().getName()+"借到了投影仪");
try {
Thread.sleep(1000);
}catch(InterruptedException ex) {
System.err.println(ex.toString());
}
synchronized(room) {
room.value=true;
System.out.println(Thread.currentThread().getName()+"预定了教室");
}
}
}
}
public class Ex10_4_seatorderedCase {
private int seatResource;
private boolean empty =true;
public void setEmpty() {
empty=true;
}
public synchronized void push(int pubResource) {
while(!empty) {
try {
wait();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
seatResource=pubResource;
empty=false;
notify();
}
public synchronized int pop() {
while(empty) {
try {
wait();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
int popResource=seatResource;
seatResource=0;
empty=true;
notify();
return popResource;
}
public static void main(String[] args) {
Ex10_4_seatorderedCase so=new Ex10_4_seatorderedCase();
SeatProcedure sp=new SeatProcedure(so);
sp.start();
SeatConsumer sc=new SeatConsumer(so);
sc.start();
SeatRelease sr=new SeatRelease(so);
sr.start();
}
}
class SeatProcedure extends Thread{
private Ex10_4_seatorderedCase so;
public SeatProcedure(Ex10_4_seatorderedCase so) {
this.so=so;
}
public void run() {
for (int i=1;i<=30;i++) {
int pubResource=i;
so.push(pubResource);
System.out.println("第"+pubResource+"号座位为空");
try {
sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
class SeatConsumer extends Thread{
private Ex10_4_seatorderedCase so;
public SeatConsumer(Ex10_4_seatorderedCase so) {
this.so=so;
}
public void run() {
for(int i=1;i<=50;i++) {
synchronized(so) {
int sh=so.pop();
if(sh!=0) {
System.out.println("学生"+i+""+"占了第"+sh+"号座位");
}else {
System.out.println("没有空座,请等待!");
}
}
try {
sleep(200);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
class SeatRelease extends Thread{
private Ex10_4_seatorderedCase so;
public SeatRelease(Ex10_4_seatorderedCase so) {
this.so=so;
}
public void run() {
try {
sleep(20000);
this.so.setEmpty();
}catch(InterruptedException e) {
e.printStackTrace();
}
for(int i=1;i<=30;i++) {
int pubResource=i;
so.push(pubResource);
System.out.println("第"+pubResource+"号座位取消预订");
try {
sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}