锁
------------------------------------------------
任何一个对象都可以是锁。信号灯,同一个参照物。
同步代码块
synchronized(lock){...}
同步方法:非静态同步方法以当前对象作为锁旗标
public synchronized void xxx(){...}
同步方法:静态同步方法以类作为锁旗标
public static synchronized void xxx(){...}
售票
---------------------------------------------
数据访问:同时卖100张票
线程安全:
class NineThreadDemo4Sale{
public static void main(String[] agrs) {
Saler s1 = new Saler("张");
Saler s2 = new Saler("王");
s1.start();
s2.start();
}
}
class Saler extends Thread{
//静态跟对象无关
static int ticket=100;
private String name;
public Saler( String name){
this.name=name;
}
//run 方法
public void run(){
//循环
while(ticket>0){
//得到当前票号
int currticket =ticket;
//还剩多少张票
ticket=ticket-1;
System.out.println(name +";"+currticket);
}
}
}
锁买票
----------------------------------------------------------
class NineThreadDemo4Sale{
public static void main(String[] agrs) {
Saler s1 = new Saler("张");
Saler s2 = new Saler("王");
s1.start();
s2.start();
}
}
class Saler extends Thread{
//静态跟对象无关
//定义一个对象锁 只要它锁上 别人就要等待
static Object lock = new Object();
static int ticket=100;
private String name;
public Saler( String name){
this.name=name;
}
//run 方法
public void run(){
//循环
while(ticket>0){
//锁的用法,synchronized获得锁定权,同一时刻只能有一个人上锁
synchronized(lock){
//得到当前票号
int currticket =ticket;
//还剩多少张票
ticket=ticket-1;
}
System.out.println(name +";"+currticket);
}
}
}
锁 取票
------------------------------------
class NineThreadDemo4Sale{
public static void main(String[] agrs) {
Saler s1 = new Saler("==>张");
Saler s2 = new Saler("=====>王");
s1.start();
s2.start();
}
}
class Saler extends Thread{
//静态跟对象无关
//定义一个对象锁 只要它锁上 别人就要等待
static Object lock = new Object();
static int ticket=100;
private String name;
public Saler( String name){
this.name=name;
}
//run 方法
public void run(){
while(true){
int tick = getticket();
if(tick >0){
System.out.println( name +":"+tick);
}
else{
return;
}
}
}
//取票动作
public int getticket(){
//同步代码块
synchronized(lock){
int currticket =ticket;
ticket --;
return currticket;
}
}
}
//同步方法---------------------------------------------------
-----------------------------------------------------------------
class NineThreadDemo4Sale{
public static void main(String[] agrs) {
Saler s1 = new Saler("==>张");
Saler s2 = new Saler("=====>王");
s1.start();
s2.start();
}
}
class Saler extends Thread{
//静态跟对象无关
//定义一个对象锁 只要它锁上 别人就要等待
//static Object lock = new Object();
static int ticket=100;
private String name;
public Saler( String name){
this.name=name;
}
//run 方法
public void run(){
while(true){
int tick = getticket();
if(tick >0){
System.out.println( name +":"+tick);
}
else{
return;
}
}
}
//同步方法(锁为当前对象作为锁 ,三个Saler 每个Saler 都以自己为锁,所以要用static 修饰,静态的方法就以类作为锁)
public static synchronized int getticket(){
int currticket =ticket;
ticket --;
return currticket;
}
}
票池取票-------------------------------------
-------------------------------------------------
class NineThreadDemo6tick{
public static void main(String[] agrs) {
TicketPool pool=new TicketPool();
Saler s1=new Saler("LILI" , pool);
Saler s2=new Saler("MARRY" , pool);
s1.start();
s2.start();
}
}
//票池
class TicketPool{
private int tickets =100;
public synchronized int Getticks(){
//票号
int tick =tickets;
//票数
tickets --;
return tickets;
}
}
//并发线程
class Saler extends Thread{
//所有的售票员从一个票池售票。声明一个变量,传过来票池。
private TicketPool pool ;
private String name;
//构造函数
public Saler(String name, TicketPool pool){
this.name =name;
this.pool =pool;
}
public void run(){
while(true){
////调用取票的方法
int no =pool.Getticks();
if(no>0){
System.out.println(name+":"+no);
}
else{
return;
}
}
}
}