java===多线程

package cn.china;
class A extends Thread{
    private static int num=200;
    Object obj=new Object();
    boolean flag=true;
    public void run(){
      if(flag)
        while(true){
            synchronized (this) {
                if(num>1){
                try {
                    Thread.sleep(10);
                } catch (Exception e) {
                    // TODO: handle exception
                }
                System.out.println("num="+num--+"	"+currentThread().getName());
            }
            }
            
        }else{while(true){function();}}
    }
    public synchronized void function(){
        if(num>1)
        try {
            Thread.sleep(10);
        } catch (Exception e) {
            // TODO: handle exception
        }
        System.out.println("num="+num--+"	"+currentThread().getName());
    }
}
public class ThreadDemo1 {

    public static void main(String[] args) {
        A a1=new A();
        A a2=new A();
        a1.start();
        try{Thread.sleep(10);}catch(InterruptedException r){
            r.printStackTrace();
        }
        
        a2.start();

    }

}
package cn.china;
/**线程同步的弊端与好处
 * 弊端:相对降低了效率,因为同步外的线程都会判断同步锁,但是同步锁都在某一个时间被占用
 * 好处:解决安全问题了
 * 线程同步的前提:
 * 同步中必须有多个线程并使用同一个锁。
 * 线程安全隐患的前提:
 * 多个线程共享一个数据,处理语句多于一条*/
class Bank {
    private int sum;
    //private Object obj = new Object();
    public synchronized void add(int num){
        
            sum+=num;
            try{Thread.sleep(10);}catch(InterruptedException e){e.printStackTrace();}
            System.out.println(Thread.currentThread().getName()+"sum="+sum);
        
        
    }
}
class Cus implements Runnable{
    Bank bank=new Bank();
    public  void run() {
        for(int x=0;x<3;x++){
            
            bank.add(1000);
            
        }
    }
}
public class ThreadDemo2 {

    public static void main(String[] args) {
        Cus b1=new Cus();
        Thread t1=new Thread(b1);
        Thread t2=new Thread(b1);
        t1.start();
        t2.start();

    }

}
package cn.china;
/**wait();让线程处于冻结状态,(释放执行权和执行资格)被wait的线程会被存储到线程池中。
 * notify();唤醒线程池中的一个线程,是任意的一个。
 * notifyAll();唤醒线程池中所有线程。
 * 这些方法都必须定义在同步中,因为这些方法时用于操作线程状态的方法,
 * 必须要明确到底操作的是哪个锁上的线程。*/
class Resource{
    String name;
    String sex;
    boolean flag=false;
}
class input implements  Runnable {
    Resource r;
    public input(Resource r){
        this.r=r;
    }
    public void run() 
    {
        int x=0;

        while(true)
        {
            synchronized(r)
            {
                if(r.flag)
                    try{
                        r.wait();
                        }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
                if(x==0)
                {
                    r.name="王五";
                    r.sex="男";
                }else{
                    r.name="笑笑";
                    r.sex="女";
                }
                
                r.flag=true;
                r.notify();
            }
            x=(x+1)%2;
        }
    }    
}
class output implements Runnable{
    Resource r;
    public output(Resource r){
        this.r=r;
    }
    public void run(){
        while(true)
        {
         synchronized (r)     
            {
             if(!r.flag)
                 try{r.wait();}catch(InterruptedException e){e.printStackTrace();}
             System.out.println(r.name+"...."+r.sex);
             r.flag=false;
             r.notify();
                 }
        }    
    }
}
public class ThreadDemo3 {

    public static void main(String[] args) {
        Resource r = new Resource();
        input i = new input(r);
        output o = new output(r);
        Thread t1=new Thread(i);
        Thread t2=new Thread(o);
        t1.start();
        t2.start();

    }

}
原文地址:https://www.cnblogs.com/wangyinxu/p/6606015.html