
本文共 10944 字,大约阅读时间需要 36 分钟。
���������������������Java������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������
���������������������������������������������������������������������������������������������������������������������������������������������������Java���������synchronized������������������������������������������������������������������������������������������������������������
������������������
������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������synchronized���������
synchronized���������������������������������������������������������������������������������������������������������������������������������
- ������������������������������������������synchronized���������������������������������������������������������������������������������������������������������������
- ������������������������������������synchronized���Obj���{}������������������������������������������������������������������������������������������������������������������
������������������
������������������
package com.syn;public class UnsafeBuyTicket { private int count = 10; // ������������ boolean flag = true; // ������������ public static void main(String[] args) { BuyTicket station = new BuyTicket(); new Thread(station, "���������").start(); new Thread(station, "���������").start(); new Thread(station, "���").start(); } class BuyTicket implements Runnable { private int count = 10; private boolean blocking = true; @Override public void run() { while (blocking) { buyTicket(); } } private synchronized void buyTicket() { if (count <= 0) { blocking = false; System.out.println("������������"); return; } // ������������������ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "���������" + (count--) + "������"); } }}
������������������
package com.syn;public class UnsafeBank { public static void main(String[] args) { Account account = new Account(1000, "������"); Drawing me = new Drawing(account, 50, "���"); Drawing girl = new Drawing(account, 100, "���"); me.start(); girl.start(); } class Account { int money; // ������ String cardName; // ������ Account(int money, String cardName) { this.money = money; this.cardName = cardName; } } class Drawing extends Thread { Account account; int drawingMoney; String name; Drawing(Account account, int drawingMoney, String name) { super(name); this.account = account; this.drawingMoney = drawingMoney; } @Override public void run() { synchronized (account) { if (account.money - drawingMoney < 0) { System.out.println(getName() + "������������"); return; } // ������������������ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // ������������������ account.money -= drawingMoney; // ������������������ System.out.println(account.cardName + " ������������������ " + account.money); System.out.println(getName() + " ��������� " + drawingMoney); } } }}
������������������
package com.syn;import java.util.ArrayList;import java.util.List;public class UnsafeList { public static void main(String[] args) { Listlist = new ArrayList<>(); for (int i = 0; i < 1000; i++) { new Thread(() -> { synchronized (list) { list.add(Thread.currentThread().getName()); } }).start(); } try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("list������������: " + list.size()); }}
������������������ReentrantLock
ReentrantLock������������������������������������������������������synchronized���������������ReentrantLock������������������������������������������������������������������������������������������������������������������������������������
package com.lock;import java.util.concurrent.locks.ReentrantLock;public class TestLock { public static void main(String[] args) { TestTicket lockTest = new TestTicket(); new Thread(lockTest, "������").start(); new Thread(lockTest, "������").start(); new Thread(lockTest, "������").start(); } static class TestTicket implements Runnable { private final ReentrantLock lock = new ReentrantLock(); @Override public void run() { while (true) { lock.lock(); try { if (ticketRemaining > 0) { System.out.println(Thread.currentThread().getName() + "������������" + ticketRemaining-- + "������"); } else { System.out.println("������������"); break; } } finally { lock.unlock(); } try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } private int ticketRemaining = 10; }}
���������������������
������������������������������������������������-���������������������������������������������������������������Java���������wait/notify���������������������������
wait/notify������
- wait()������������������������������������������������������������������������������������������������������
- notify()���������������������������
- notifyAll()���������������������������������������������������������
���������-���������������������
package com.pc;import java.util.ArrayList;public class TestPC { public static void main(String[] args) { ProductQueue queue = new ProductQueue(); Producer p1 = new Producer(queue, "���01"); p1.start(); p2 = new Consumer(queue, "���01"); p2.start(); } static class ProductQueue { private final ArrayListqueue = new ArrayList<>(); private int size = 0; void push(Product p) { System.out.println(Thread.currentThread().getName() + " ������������������������" + p); queue.add(p); size++; if (size == 10) { wait(); } notify(); } Product pop() { System.out.println(Thread.currentThread().getName() + " ���������������������"); if (size == 0) { wait(); } Product p = queue.remove(size--; System.out.println(Thread.currentThread().getName() + " ���������������: " + p); notify(); return p; } } static class Producer extends Thread { ProductQueue queue; String name; Producer(ProductQueue queue, String name) { super(name); this.queue = queue; this.name = name; } @Override public void run() { System.out.println(getName() + " ���������������" + Thread getId()); while (true) { Product p = new Product++; queue.push(p); System.out.println(getName() + " ������������" + p.toString()); try { sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } } static class Consumer extends Thread { ProductQueue queue; String name; Consumer(ProductQueue queue, String name) { super(name); this.queue = queue; this.name = name; } @Override public void run() { System.out.println(getName() + " ���������������" +.EventQueue.getId()); while (true) { if (queue.size() > 0) { Product p = queue.pop(); System.out.println(getName() + " ������������" + p); sleep(1); } else { System.out.println(getName() + " ������������..."); sleep(1); } } } } static class Product { private final int id; private final String type; Product(int id) { this.id = id; } public String toString() { return "Product ID: " + id + " - ������"; } }}
���������������������
������������������������������������������������������������������������������������������JDK���������Executor������������������������������������������
package com.pool;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class TestPool { public static void main(String[] args) { // ��������������� ExecutorService pool = Executors.newFixedThreadPool(50); // ������������ pool.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + " 108���������������������"); } }); pool.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + " 109���������������������"); } }); // ��������������� pool.shutdown(); }}
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
