多线程

循环嵌套导致死锁:

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

 

全部评论

相关推荐

刚刷到字节跳动官方发的消息,确实被这波阵仗吓了一跳。在大家还在纠结今年行情是不是又“寒冬”的时候,字节直接甩出了史上规模最大的转正实习计划——ByteIntern。咱们直接看几个最硬的数,别被花里胡哨的宣传词绕晕了。首先是“量大”。全球招7000多人是什么概念?这几乎是把很多中型互联网公司的总人数都给招进来了。最关键的是,这次的资源分配非常精准:研发岗给了4800多个Offer,占比直接超过六成。说白了,字节今年还是要死磕技术,尤其是产品和AI领域,这对于咱们写代码的同学来说,绝对是今年最厚的一块肥肉。其次是大家最关心的“转正率”。官方直接白纸黑字写了:整体转正率超过50%。这意味着只要你进去了,不划水、正常干,每两个人里就有一个能直接拿校招Offer。对于2027届(2026年9月到2027年8月毕业)的同学来说,这不仅是实习,这简直就是通往大厂的快捷通道。不过,我也得泼盆冷水。坑位多,不代表门槛低。字节的实习面试出了名的爱考算法和工程实操,尤其是今年重点倾斜AI方向,如果你简历里有和AI相关的项目,优势还是有的。而且,转正率50%也意味着剩下那50%的人是陪跑的,进去之后的考核压力肯定不小。一句话总结: 27届的兄弟们,别犹豫了。今年字节这是铁了心要抢提前批的人才,现在投递就是占坑。与其等到明年秋招去千军万马挤独木桥,不如现在进去先占个工位,把转正名额攥在手里。
喵_coding:别逗了 50%转正率 仔细想想 就是转正与不转正
哪些公司开暑期实习了?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务