{"rsdb":{"rid":"158780","subhead":"","postdate":"0","aid":"119011","fid":"54","uid":"1","topic":"1","content":"

\u540c\u6b65\u9501\u5206\u7c7b<\/p>

\u5bf9\u8c61\u9501\uff08this\uff09<\/p>

\u7c7b\u9501\uff08\u7c7b\u7684\u5b57\u8282\u7801\u6587\u4ef6\u5bf9\u8c61\u5373\u7c7b\u540d.class\uff09<\/p>

\u5b57\u7b26\u4e32\u9501\uff08\u6bd4\u8f83\u7279\u522b\uff09<\/p>

\u5e94\u7528\u573a\u666f<\/p>

\u5728\u591a\u7ebf\u7a0b\u4e0b\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u5b89\u5168\u64cd\u4f5c\u3002<\/p>

\u9700\u6c42\uff1a\u542f\u52a85\u4e2a\u7ebf\u7a0b\u5bf9\u5171\u4eab\u8d44\u6e90total\u8fdb\u884c\u5b89\u5168\u64cd\u4f5c\u3002<\/p>

\u540c\u6b65\u9501\u5728\u591a\u7ebf\u7a0b\u5355\u4f8b\u6a21\u5f0f\u4e0b\u7684\u4f7f\u7528<\/p>

\u4ee5\u4e0a\u4e09\u7c7b\u540c\u6b65\u9501\u90fd\u53ef\u4ee5\u3002<\/p>


package cn.myThread;<\/p>

public class MyThread implements Runnable {
    private static int total = 10;
    @Override
    public void run() {
        synchronized (this){ \/\/\u4f7f\u7528this\u5bf9\u8c61\u9501
        \/\/synchronized (MyThread.class){ \/\/\u4f7f\u7528MyThread.class\u7c7b\u9501
        \/\/synchronized (""){\/\/\u4f7f\u7528\u5b57\u7b26\u4e32\u9501
            System.out.println(Thread.currentThread().getName() + "\u6b63\u5728\u8fd0\u884c");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            total--;
            System.out.println(total);
            System.out.println(Thread.currentThread().getName() + "\u7ebf\u7a0b\u7ed3\u675f");
        }
    }
 }
 <\/p>

package cn.test;<\/p>

import cn.myThread.MyThread;<\/p>

public class TestMyThread {
    public static void main(String[] args){
        MyThread myThread = new MyThread();
        Thread thread = null;
        for (int i = 1 ; i <= 5 ; i++){
            thread = new Thread(myThread,"\u7ebf\u7a0b"+i); \/\/\u5f00\u542f5\u4e2a\u7ebf\u7a0b\uff0c\u4f20\u5165\u540c\u4e00\u4e2a\u5bf9\u8c61
            thread.start();
        }
    }
 }
 <\/p>

\u7ebf\u7a0b1\u6b63\u5728\u8fd0\u884c<\/p>

9<\/p>

\u7ebf\u7a0b1\u7ebf\u7a0b\u7ed3\u675f<\/p>

\u7ebf\u7a0b3\u6b63\u5728\u8fd0\u884c<\/p>

8<\/p>

\u7ebf\u7a0b3\u7ebf\u7a0b\u7ed3\u675f<\/p>

\u7ebf\u7a0b5\u6b63\u5728\u8fd0\u884c<\/p>

7<\/p>

\u7ebf\u7a0b5\u7ebf\u7a0b\u7ed3\u675f<\/p>

\u7ebf\u7a0b2\u6b63\u5728\u8fd0\u884c<\/p>

6<\/p>

\u7ebf\u7a0b2\u7ebf\u7a0b\u7ed3\u675f<\/p>

\u7ebf\u7a0b4\u6b63\u5728\u8fd0\u884c<\/p>

5<\/p>

\u7ebf\u7a0b4\u7ebf\u7a0b\u7ed3\u675f
 <\/p>

\u5206\u6790\uff1a\u4ece\u8fd0\u884c\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa5\u4e2a\u7ebf\u7a0b\u4e32\u884c\u6267\u884c\u540c\u6b65\u9501\u91cc\u9762\u7684\u4ee3\u7801\uff0c\u56e0\u4e3a5\u4e2a\u7ebf\u7a0b\u4e2d\u7684\u540c\u6b65\u9501\u5bf9\u8c61this\u6307\u5411\u540c\u4e00\u4e2a\u7684\u5bf9\u8c61\uff08\u540c\u6b65\u9501\u5bf9\u8c61MyThread.class\u7c7b\u9501\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3001\u540c\u6b65\u9501\u5bf9\u8c61 \u201d\u201d \u5b57\u7b26\u4e32\u9501\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff09,\u6240\u4ee55\u4e2a\u7ebf\u7a0b\u4f1a\u4e32\u884c\u6267\u884c\u540c\u6b65\u9501\u91cc\u9762\u7684\u4ee3\u7801\u3002<\/p>

\u540c\u6b65\u9501\u5728\u591a\u7ebf\u7a0b\u591a\u4f8b\u6a21\u5f0f\u4e0b\u7684\u4f7f\u7528<\/p>

\u9519\u8bef\u7528\u6cd5<\/p>


package cn.myThread;<\/p>

public class MyThread implements Runnable {
    private static int total = 10;
    @Override
    public void run() {
        synchronized (this){\/\/\u4f7f\u7528this\u5bf9\u8c61\u9501
            System.out.println(Thread.currentThread().getName() + "\u6b63\u5728\u8fd0\u884c");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            total--;
            System.out.println(total);
            System.out.println(Thread.currentThread().getName() + "\u7ebf\u7a0b\u7ed3\u675f");
        }
    }
 }
 <\/p>

package cn.test;<\/p>

 import cn.myThread.MyThread;<\/p>

 public class TestMyThread {
    public static void main(String[] args){
        Thread thread = null;
        for (int i = 1 ; i <= 5 ; i++){
            thread = new Thread(new MyThread(),"\u7ebf\u7a0b"+i);\/\/\u5f00\u542f5\u4e2a\u7ebf\u7a0b\uff0c\u4f20\u51655\u4e2a\u4e0d\u540c\u5bf9\u8c61
            thread.start();
    &n","orderid":"0","title":"Java\u540c\u6b65\u9501\u7684\u6b63\u786e\u4f7f\u7528(\u4e00)","smalltitle":"","mid":"0","fname":"linux\u7f16\u7a0b\u57fa\u7840","special_id":"0","bak_id":"0","info":"0","hits":"8762","pages":"3","comments":"0","posttime":"2017-09-19 14:21:05","list":"1505802065","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Java<\/A> \u540c\u6b65<\/A> \u6b63\u786e<\/A> \u4f7f\u7528<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"113.108.110.181","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Java\u540c\u6b65\u9501\u7684\u6b63\u786e\u4f7f\u7528","lastview":"1713423789","digg_num":"3514","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}