多线程

循环嵌套导致死锁:

class Test implements Runnable
{
	private boolean flag;
	Test(boolean flag)
	{
		this.flag = flag;
	}

	public void run()
	{
		
		if(flag)
		{
			while(true)
				synchronized(MyLock.locka)
				{
					System.out.println(Thread.currentThread().getName()+"..if   locka....");
					synchronized(MyLock.lockb)				{
						
						System.out.println(Thread.currentThread().getName()+"..if   lockb....");
					}
				}
		}
		else
		{
			while(true)			
				synchronized(MyLock.lockb)
				{
					System.out.println(Thread.currentThread().getName()+"..else  lockb....");
					synchronized(MyLock.locka)
					{
						System.out.println(Thread.currentThread().getName()+"..else   locka....");
					}
				}
		}

	}

}

class MyLock
{
	public static final Object locka = new Object();
	public static final Object lockb = new Object();
}

class DeadLockTest 
{
	public static void main(String[] args) 
	{
		Test a = new Test(true);
		Test b = new Test(false);

		Thread t1 = new Thread(a);
		Thread t2 = new Thread(b);
		t1.start();
		t2.start();
	}
}

多线程间通信:

package p2;

class Res {
	String name;
	int count = 1;
	boolean flag;

	public synchronized void set(String name) {
		while (flag) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.name = name + count;
		count++;
		System.out.println(Thread.currentThread().getName() + "--生产者--" + this.name);
		flag = true;
		notifyAll();
	}

	public synchronized void get() {
		while (!flag) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println(Thread.currentThread().getName() + "---消费者---" + this.name);
		flag = false;
		notifyAll();
	}
}

class Producter implements Runnable {

	private Res r;

	public Producter(Res r) {
		this.r = r;
	}

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			r.set("面包");
		}
	}
}

class Customer implements Runnable {

	private Res r;

	public Customer(Res r) {
		this.r = r;
	}

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

}

public class Test1 {
	public static void main(String[] args) {
		Res r = new Res();
		/*Producter producter = new Producter(r);
		Producter producter2 = new Producter(r);
		Customer customer = new Customer(r);
		Customer customer2 = new Customer(r);
		Thread t1 = new Thread(producter);
		Thread t2 = new Thread(producter2);
		Thread t3 = new Thread(customer);
		Thread t4 = new Thread(customer2);
		
		t1.start();
		t2.start();
		t3.start();
		t4.start();*/
		
		
		new Thread(new Producter(r)).start();
		new Thread(new Producter(r)).start();
		new Thread(new Producter(r)).start();
		new Thread(new Producter(r)).start();
		
		new Thread(new Customer(r)).start();
		new Thread(new Customer(r)).start();
		new Thread(new Customer(r)).start();
		new Thread(new Customer(r)).start();
	}
}
package p2;

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

class Res2 {
	private String name;
	private int count = 1;
	private boolean flag;
	private Lock lock = new ReentrantLock();
	private Condition Producter_con = lock.newCondition();
	private Condition Customer_con = lock.newCondition();

	public void set(String name) {
		lock.lock();
		try {
			while (flag) {
				Producter_con.await();
			}
			this.name = name + count;
			count++;
			System.out.println(Thread.currentThread().getName() + "---生产者---" + this.name);
			flag = true;
			Customer_con.signal();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}

	public void get() {
		lock.lock();
		try {
			while (!flag) {
				Customer_con.await();
			}
			System.out.println(Thread.currentThread().getName() + "--消费者--" + this.name);
			flag = false;
			Producter_con.signal();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
}

class Producter2 implements Runnable {

	private Res2 r;

	public Producter2(Res2 r) {
		this.r = r;
	}

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			r.set("面包");
		}
	}
}

class Customer2 implements Runnable {

	private Res2 r;

	public Customer2(Res2 r) {
		this.r = r;
	}

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

public class Test2 {
	public static void main(String[] args) {
		Res2 r = new Res2();
		Producter2 producter = new Producter2(r);
		Customer2 customer = new Customer2(r);

		new Thread(producter).start();

		new Thread(producter).start();
		new Thread(producter).start();
		new Thread(producter).start();

		new Thread(customer).start();
		new Thread(customer).start();
		new Thread(customer).start();

		new Thread(customer).start();
	}
}
package p2;

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

class Resourse {

	String name;
	String[] arr = new String[10];
	Lock lock = new ReentrantLock();
	Condition In_con = lock.newCondition();
	Condition Out_con = lock.newCondition();
	int outIndex, inIndex, count, num;

	public void set(String name) {
		lock.lock();
		try {
			while (count == arr.length) {
				In_con.await();
			}
			this.name = name + num;
			arr[inIndex] = this.name;
			inIndex = inIndex == arr.length - 1 ? 0 : inIndex + 1;
			count++;
			num++;
			System.out.println(Thread.currentThread().getName() + "--生产者--" + this.name);
			Out_con.signal();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}

	public void get() {
		lock.lock();
		try {
			while (count == 0) {
				Out_con.await();
			}
			System.out.println(Thread.currentThread().getName() + "---消费者---" + arr[outIndex]);
			outIndex = outIndex == arr.length - 1 ? 0 : outIndex + 1;
			count--;
			In_con.signal();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}

}

class Product implements Runnable {
	private Resourse r = new Resourse();

	public Product(Resourse r) {
		this.r = r;
	}

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			r.set("面包");
		}
	}

}

class Custom implements Runnable {
	private Resourse r = new Resourse();

	public Custom(Resourse r) {
		this.r = r;
	}

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

}

public class Test4 {
	public static void main(String[] args) {
		Resourse r = new Resourse();
		Product product = new Product(r);
		Custom custom = new Custom(r);

		new Thread(product).start();
		new Thread(product).start();
		new Thread(product).start();

		new Thread(custom).start();
		new Thread(custom).start();
		new Thread(custom).start();
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值