同步函数
package ticket;
class Ticket1 implements Runnable{//extends Thread{
private int num=100;
//Object obj=new Object();
public void run(){ //synchronized void run(){
// 不要把不该同步的代码同步,此时会出现问题,在这种情况下,0线程进来永远也出不去
//可以把需要同步的的代码封装起来,在加同步关键字,然后在run方法中调用。
while(true){
show();
}
}
public synchronized void show(){
if (num>0) {
try {Thread.sleep(10);} catch (InterruptedException e) {}
System.out.println(Thread.currentThread().getName()+"----sale...."+num--);
}
}
}
public class synFunctionLockDemo {
public static void main(String[] args) {
Ticket1 t=new Ticket1();//创建一个线程任务
//Ticket tt=new Ticket();
package ticket;
class Ticket1 implements Runnable{//extends Thread{
private int num=100;
//Object obj=new Object();
public void run(){ //synchronized void run(){
// 不要把不该同步的代码同步,此时会出现问题,在这种情况下,0线程进来永远也出不去
//可以把需要同步的的代码封装起来,在加同步关键字,然后在run方法中调用。
while(true){
show();
}
}
public synchronized void show(){
if (num>0) {
try {Thread.sleep(10);} catch (InterruptedException e) {}
System.out.println(Thread.currentThread().getName()+"----sale...."+num--);
}
}
}
public class synFunctionLockDemo {
public static void main(String[] args) {
Ticket1 t=new Ticket1();//创建一个线程任务
//Ticket tt=new Ticket();