java 线程学习(1)

Java多线程同步机制详解
import java.io.*; /* class Demo extends Thread { Demo(String name) { super(name);//给线程自定义命名 } public void run() { for(int i=0;i<60;i++) System.out.println(Thread.currentThread()+" "+this.getName()+" run-----"+i);//获取当前线程 } } class Test //发送端 { public static void main(String[] args) { Demo d=new Demo("myThread"); d.start();//开启线程并执行该线程的run方法 //d.start();//当这个线程正在运行时,不能再启动 //d.run();//仅仅是对象调用方法,而线程创建了并没有运行 for(int i=0;i<60;i++) System.out.println("hello world!!"+i); } }*/ /*synchrionized 块 class Ticket implements Runnable { private int ticket=1000; Object obj=new Object(); public void run() { while(true) { synchronized(obj) { if(ticket>0) { System.out.println(Thread.currentThread().getName()+"---sale "+ticket--); } } } } } class Test { public static void main(String[] args) { Ticket t=new Ticket(); Thread t1=new Thread(t); Thread t2=new Thread(t); Thread t3=new Thread(t); Thread t4=new Thread(t); t1.start(); t2.start(); t3.start(); t4.start(); } } */ /* synchronized 方法 class Bank { private int sum; //Object obj=new Object(); public synchronized void add(int n) { // synchronized(obj) // { sum=sum+n; try{Thread.sleep(10);}catch(Exception e){} System.out.println("sum--"+sum); // } } } class Cus implements Runnable { private Bank b=new Bank(); public void run() { for(int i=0;i<3;i++) b.add(100); } } class Test { public static void main(String[] args) { Cus c=new Cus(); Thread t1=new Thread(c); Thread t2=new Thread(c); t1.start(); t2.start(); } } */ /* //同步函数调用的哪一个锁呢? //函数需要被对象调用,那么函数都有一个所属对象引用,就是this //所以同步函数使用的锁是this 以下为验证 class Ticket implements Runnable { private int ticket=100; Object obj=new Object(); boolean flag=true; public void run() { if(flag) { while(true) { synchronized(this) //两个线程为同一个锁:安全 换成obj的话为两个锁:不完全 { if(ticket>0) { try{Thread.sleep(10);}catch(Exception e){} System.out.println(Thread.currentThread().getName()+"---sale "+ticket--); } } } } else while(true) show(); } public synchronized void show() { if(ticket>0) { try{Thread.sleep(10);}catch(Exception e){} System.out.println(Thread.currentThread().getName()+"----show---"+ticket--); } } } class Test { public static void main(String[] args) { Ticket t=new Ticket(); Thread t1=new Thread(t); Thread t2=new Thread(t); t1.start(); try{Thread.sleep(10);}catch(Exception e){} t.flag=false; t2.start(); } } */ /* //如果同步函数是静态函数,使用的锁是什么呢? //不在是this,因为静态方法中也不可以定义this //静态进内存是,内存中没有本类对象,但是一定有该类对应的字节码文件对象 //类名.class 该对象的类型是Class //静态的同步方法,使用的锁是该方法所在类的字节码文件对象:类名.class class Ticket implements Runnable { private static int ticket=100; Object obj=new Object(); boolean flag=true; public void run() { if(flag) { while(true) { synchronized(Ticket.class) //两个线程为同一个锁:安全 换成obj的话为两个锁:不完全 { if(ticket>0) { try{Thread.sleep(10);}catch(Exception e){} System.out.println(Thread.currentThread().getName()+"---sale "+ticket--); } } } } else while(true) show(); } public static synchronized void show() { if(ticket>0) { try{Thread.sleep(10);}catch(Exception e){} System.out.println(Thread.currentThread().getName()+"----show---"+ticket--); } } } class Test { public static void main(String[] args) { Ticket t=new Ticket(); Thread t1=new Thread(t); Thread t2=new Thread(t); t1.start(); try{Thread.sleep(10);}catch(Exception e){} t.flag=false; t2.start(); } } */
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值