您的位置:首页 > 其它

多线程 同步问题

2015-06-24 23:06 120 查看
需求一

  模拟四个窗口同时卖100张火车票的问题

package work.thread;

import java.util.Objects;

/**
* Created by coolkid on 2014/12/17 0017.
*/

class Ticket implements Runnable{

//票数
private int count = 100;

private Object object = new Object();
private void saleTicket(){
while (true){
synchronized (object){
if (count > 0){
count--;
System.out.println(Thread.currentThread().getName()+" 卖出第 "+(100-count)+" 张票");
}else {
break;
}
}
}

}
@Override
public void run() {
saleTicket();
}
}
public class Thread01 {
public static void main(String[] args) {
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);

t1.start();
t2.start();
t3.start();
t4.start();
}
}


对于卖票的过程,必须使用同步代码段,否则就会出现同一张票被多次售卖的问题

需求二

  生产者消费者问题

package work.thread;

/**
* Created by coolkid on 2014/12/17 0017.
*/

class Resource02{
private String name;
private String sex;
private int count = 0;

public synchronized void set(String name,String sex){
while (count>10){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name;
this.sex = sex;
count++;
this.notifyAll();
}

public synchronized void out()  {
while (count<1){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"\tname = " + name+"\nsex = " + sex);
count--;
this.notifyAll();
}
}

class Input02 implements Runnable{
private Resource02 r;

public Input02(Resource02 r) {
this.r = r;
}

@Override
public void run() {
int x = 0;
while (true){
if (x == 0){
r.set("mike","nan");
}else {
r.set("莉莉","女..........");
}
x = (x+1)%2;
}

}
}

class Output02 implements Runnable{
private Resource02 r;

public Output02(Resource02 r) {
this.r = r;
}

@Override
public void run() {
while (true){
r.out();
}
}
}

public class Thread02 {
public static void main(String[] args) {
//资源
Resource02 r = new Resource02();
//生产者
Input02 i = new Input02(r);
//消费者
Output02 o = new Output02(r);

//执行路径
Thread t1 = new Thread(i);
Thread t2 = new Thread(i);

Thread t3 = new Thread(o);

t1.start();
t2.start();
t3.start();

}
}


需求三

  使用监视器实现生产者消费者问题

package work.thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* Created by coolkid on 2014/12/17 0017.
*/

class Resource02{
private String name;
private String sex;
private int count = 0;

Lock lock = new ReentrantLock();
Condition pro_con = lock.newCondition();
Condition con_con = lock.newCondition();

public void set(String name,String sex){
lock.lock();
try {
while (count>10){
pro_con.await();
}
this.name = name;
this.sex = sex;
count++;
con_con.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}

public void out()  {
lock.lock();
try {
while (count<1){
con_con.await();
}
System.out.println(Thread.currentThread().getName()+"\tname = " + name+"\tsex = " + sex);
count--;
pro_con.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}

class Input02 implements Runnable{
private Resource02 r;

public Input02(Resource02 r) {
this.r = r;
}

@Override
public void run() {
int x = 0;
while (true){
if (x == 0){
r.set("mike","nan");
}else {
r.set("莉莉","女..........");
}
x = (x+1)%2;
}

}
}

class Output02 implements Runnable{
private Resource02 r;

public Output02(Resource02 r) {
this.r = r;
}

@Override
public void run() {
while (true){
r.out();
}
}
}

public class Thread02 {
public static void main(String[] args) {
//资源
Resource02 r = new Resource02();
//生产者
Input02 i = new Input02(r);
//消费者
Output02 o = new Output02(r);

//执行路径
Thread t1 = new Thread(i);
Thread t2 = new Thread(i);

Thread t3 = new Thread(o);

t1.start();
t2.start();
t3.start();

}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: