多线程

循环嵌套导致死锁:

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();
	}
}

 

全部评论

相关推荐

点赞 评论 收藏
分享
头像
11-09 17:30
门头沟学院 Java
TYUT太摆金星:我也是,好几个华为的社招找我了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务