java 如何編寫多線程的代碼


 

線程是干活的
所以線程一定是Thread,或者該線程實現Runnable接口
多線程是競爭關系,所以多個線程競爭同一個資源,也就是同一個對象
所以這個競爭對象放到Thread中
即:
// resources是競爭資源
Resources resources = new Resources();
Thread1 thread1 = new Thread1(resources);
Thread2 thread2 = new Thread2(resources);

thread1.start();
thread2.start();

----------------------------------------------------------------------
class Thread1 implements Runnable {
  Resources resources = null;
  Thread1(Resources resources) {
    this.resources = resources;
  }

  public void run() {
    //這個methodA方法時Resources里面的競爭資源方法
    resources.methodA();
  }
}

class Thread2 implements Runnable {
  Resources resources = null;
  Thread2(Resources resources) {
    this.resources = resources;
  }

  public void run() {
    //這個methodA方法時Resources里面的競爭資源方法
    resources.methodA();
  }

}

class Resources {
  private int count = 100;
  //多線程去干活了,它們爭着搶着去執行競爭資源里面的方法,所以這個方法區域需要加鎖
  public synchronized void methodA() {
    if(count > 0) {
      count--;
     }
  }
}

 例子:

package Thread;

public class MultiThread {

    public static void main(String[] args) {
        //resources就是競爭資源對象
        Resources resources = new Resources();
        Runnable1 runnable1 = new Runnable1(resources);

        for(int i = 0; i <100; i++) {
            // 這里是創建多線程去執行任務
            //多線程是競爭關系,所以多個線程競爭同一個資源,也就是同一個對象
            //所以這個競爭對象放到Thread中
            new Thread(runnable1,"Thread"+i).start();
        }
    }

}

class Resources {
    private int count = 100;
    
    //多線程去干活了,它們爭着搶着去執行競爭資源里面的方法,所以這個方法區域需要加鎖
    public synchronized  void methodA() {
        if(count > 0) {
            count--;
        }
        System.out.println(Thread.currentThread().getName() + "  " +"count:"+count);
    }
}

class Runnable1 implements Runnable {
    Resources resources = null;
    Runnable1(Resources resources) {
        this.resources = resources;
    }
    
    public void run() {
        //這個methodA方法時Resources里面的競爭資源方法
        resources.methodA();
    }
    
}

 .....

 

 多線程可以同時訪問同個對象的不同方法嗎?

例子:

public class Test66 {
    public static void main(String[] args) {
        A a = new A();
        Thread1 thread1 = new Thread1(a);
        Thread2 thread2 = new Thread2(a);
        
        thread1.start();
        thread2.start();
    }
}

class A{
    public synchronized void method1() throws InterruptedException {
        System.out.println("進入method1方法睡5秒");
        Thread.sleep(5000);
    }
    
    public synchronized void method2() throws InterruptedException {
        System.out.println("進入method1方法睡2秒");
        Thread.sleep(2000);
    }
}

class Thread1 extends Thread {
    A a;
    public Thread1(A a) {
        this.a = a;
    }
    
    @Override
    public void run() {
        try {
            a.method1();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Thread2 extends Thread {
    A a;
    public Thread2(A a) {
        this.a = a;
    }
    
    @Override
    public void run() {
        try {
            a.method2();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

 

 

 修改一下 :講method2的 synchronized 去掉

class A{
    public synchronized void method1() throws InterruptedException {
        System.out.println("進入method1方法睡5秒");
        Thread.sleep(5000);
        System.out.println("進入method1結束");
    }
    
    public void method2() throws InterruptedException {
        System.out.println("進入method2方法睡2秒");
        Thread.sleep(2000);
        System.out.println("進入method2結束");
    }
}

 

 

因此得出結論:同個對象的兩個同步方法不能並發執行,也就是一個線程獲取了一個對象的鎖之后,對應這個對象的其他同步方法也被鎖住,其他線程只能等待。若方法沒有被synchronized 修飾,則可以多線程並發執行

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM