米课wordpress建站,平面设计师需要学习什么,一个好的网站需要具备什么,网站怎么搬家synchronized方法和synchronized块
synchronized方法必须获得对象的锁才能执行#xff0c;否则线程阻塞#xff0c;方法一旦执行#xff0c;就独占此锁#xff0c;直到方法返回才释放锁#xff0c;后面被阻塞的线程才能获得这个锁#xff0c;继续执行
synchronized会影…synchronized方法和synchronized块
synchronized方法必须获得对象的锁才能执行否则线程阻塞方法一旦执行就独占此锁直到方法返回才释放锁后面被阻塞的线程才能获得这个锁继续执行
synchronized会影响效率
同步块synchronized(Obj){}Obj是同步监听器第一个线程访问所定Obj完后解锁Obj第二个才能访问
同步方法弊端方法里面需要修改的内容才需要锁锁的太多浪费资源
package com.wuming.syn.synch;
//不安全的买票
public class UnSafeBuyTicket {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 ticketNums10;boolean flagtrue;//停止方式/*** When an object implementing interface codeRunnable/code is used* to create a thread, starting the thread causes the objects* coderun/code method to be called in that separately executing* thread.* p* The general contract of the method coderun/code is that it may* take any action whatsoever.** see Thread#run()*/Overridepublic void run() {//买票while(flag){try {buy();} catch (InterruptedException e) {e.printStackTrace();}}}//synchronized同步方法锁的是this BuyTicketprivate synchronized void buy() throws InterruptedException {//判断是否有票if (ticketNums0){flagfalse;return;}//模拟延时Thread.sleep(100);//买票System.out.println(Thread.currentThread().getName()拿到ticketNums--);//加了synchronized安全/* 苦逼的我拿到10可恶的黄牛党拿到9可恶的黄牛党拿到8牛逼的你们拿到7可恶的黄牛党拿到6苦逼的我拿到5苦逼的我拿到4可恶的黄牛党拿到3牛逼的你们拿到2可恶的黄牛党拿到1*/}
}package com.wuming.syn;
//不安全的取钱
//两个人去银行取钱账户
public class UnSafeBank {public static void main(String[] args) {//账户Account account new Account(100,结婚基金);Drawing you new Drawing(account,50,你);Drawing girlFriend new Drawing(account,100,girlFriend);you.start();girlFriend.start();}}
//账户
class Account{int money;//余额String name;//卡名public Account(int money, String name) {this.money money;this.name name;}
}//银行模拟取款
class Drawing extends Thread{Account account;//账户//取了多少钱int drawingMoney;//现在手里有多少钱int nowMoney;public Drawing(Account account, int drawingMoney, String name) {super(name);//线程namethis.account account;this.drawingMoney drawingMoney;this.nowMoney nowMoney;}
//取钱//synchronized默认锁的是本身Overridepublic void run() {//锁的对象是变化的量增删改;改成this就是指银行线程不安全如下/* 结婚基金余额为-50结婚基金余额为-50你手里的钱50girlFriend手里的钱100*/synchronized (account){//判断有没有钱if(account.money-drawingMoney0){System.out.println(Thread.currentThread().getName()钱不够取不了);return;}//sleep可以放大问题的发生性try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}//卡内余额余额-你取的钱account.moneyaccount.money-drawingMoney;//你手里的钱nowMoneynowMoneydrawingMoney;System.out.println(account.name余额为account.money);//Thread.currentThread().getName()this.getName();System.out.println(this.getName()手里的钱nowMoney);}//使用同步块 synchronized (account)线程安全/* 结婚基金余额为50你手里的钱50girlFriend钱不够取不了*/}
}package com.wuming.syn.synch;import java.util.ArrayList;
import java.util.List;//线程不安全的集合
public class UnsafeList {public static void main(String[] args) {ListString listnew ArrayListString();for (int i 0; i 10000; i) {new Thread(()-{synchronized (list){//线程安全list.add(Thread.currentThread().getName());}}).start();}try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(list.size());//每次运行都不够10000线程不安全两个线程同时操作同一个位置//两个数组添加到同一个位置就会覆盖掉元素就会少}
}10000