JAVA 线程安全 秘笈

 

 线程安全,归根上来说,其实就是属性所存储的数据安全。

JAVA属性主要为三种,依次为:类属性、实例属性、方法属性。

下面的代码来帮助理解这三种属性,你可以选择性地释放注释的代码,然后运行。

建议运得下面代码之前,先在搜索引擎上对线程与线程安全做一些较为深入地了解。

 

/******************************************************************************* 
 * Copyright (c) 2005-2008 AngelSoftware , Ltd. 
 * All rights reserved.  
******************************************************************************
*/


package com.angelframework.common.temp;
/**
 * <pre>
 *  Description:    
 *    TODO 
 * 
 *  Revision History:
 *  01-四月-08 fity.wang        initial version.
 * 
 *    Authors    Information:
 *  fity.wang        fity.wang@gmail.com
 * </pre>
 
*/

public   class  ThreadSafe1
{
    
/**类级别属性*/
    
public static String classField = "classField";
    
/**实例级别属性*/
    
public String instanceField = "instanceField";
    
    
public/*synchronized*/ void method()
    
{
        String methodField 
= "methodField";
        
/*synchronized (instanceField)*/
        
{
            
if ("线程1".equals(Thread.currentThread().getName()))
            
{
                
try
                
{
                    
/*synchronized (instanceField)*/
                    
{
                        
/*instanceField = instanceField+"线程1";*/
                        
/*Thread.sleep(2000);*/
                        methodField 
= methodField + "线程1";
                        
/**/instanceField = instanceField+"线程1";/**/
                        classField 
= classField+"线程1";
                        System.
out.println("线程1 Sleep !");
                        
/**/Thread.sleep(2000);/**/
                        System.
out.println("线程1@@@ classField: "+classField+" instanceField: "+instanceField+" methodField: "+methodField);
                    }

                    
                }

                
catch (InterruptedException e)
                
{
                    
// TODO 将错误记入ERROR日志
                    e.printStackTrace();
                }

            }

            
if ("线程2".equals(Thread.currentThread().getName()))
            
{
                
try
                
{
                    methodField 
= methodField + "线程2";
                    instanceField 
= instanceField+"线程2";
                    classField 
= classField+"线程2";
                    System.
out.println("线程2 Sleep!");
                    Thread.sleep(
1000);
                    System.
out.println("线程2@@@ classField: "+classField+" instanceField: "+instanceField+" methodField: "+methodField);
                }

                
catch (InterruptedException e)
                
{
                    
// TODO 将错误记入ERROR日志
                    e.printStackTrace();
                }

            }

            
if ("线程3".equals(Thread.currentThread().getName()))
            
{
                methodField 
= methodField + "线程3";
                instanceField 
= instanceField+"线程3";
                classField 
= classField+"线程3";
                System.
out.println("线程3@@@ classField: "+classField+" instanceField: "+instanceField+" methodField: "+methodField);
            }

        }

        
    }

    
    
/**
     * @param args
     
*/

    
public static void main(String[] args)
    
{
        
// TODO 
        final ThreadSafe1 ts = new ThreadSafe1();
        TH1 t1 
= new TH1(ts);
        TH2 t2 
= new TH2(ts);
        TH3 t3 
= new TH3(ts);
        t1.start();
        t2.start();
        t3.start();
    }


}

/**
 * <pre>
 *  Description:    
 *    TODO 线程1
 * 
 *  Revision History:
 *  01-四月-08 fity.wang        initial version.
 * 
 *    Authors    Information:
 *  fity.wang        fity.wang@gmail.com
 * </pre>
 
*/

class  TH1 extends Thread
{
    
private ThreadSafe1 ts = null;
    
public TH1(ThreadSafe1 ts)
    
{
        super(
"线程1");
        
this.ts = ts;
    }

    
public void run()
    
{
        ts.method();
    }

}


/**
 * <pre>
 *  Description:    
 *    TODO 线程2
 * 
 *  Revision History:
 *  01-四月-08 fity.wang        initial version.
 * 
 *    Authors    Information:
 *  fity.wang        fity.wang@gmail.com
 * </pre>
 
*/

class  TH2 extends Thread
{
    
private ThreadSafe1 ts = null;
    
public TH2(ThreadSafe1 ts)
    
{
        super(
"线程2");
        
this.ts = ts;
    }

    
public void run()
    
{
        ts.method();
    }

}


/**
 * <pre>
 *  Description:    
 *    TODO 线程3
 * 
 *  Revision History:
 *  01-四月-08 fity.wang        initial version.
 * 
 *    Authors    Information:
 *  fity.wang        fity.wang@gmail.com
 * </pre>
 
*/

class  TH3 extends Thread
{
    
private ThreadSafe1 ts = null;
    
public TH3(ThreadSafe1 ts)
    
{
        super(
"线程3");
        
this.ts = ts;
    }

    
public void run()
    
{
        ts.method();
    }

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值