JAVA 线程安全 秘笈

来源:互联网 发布:ppp项目有哪些 知乎 编辑:程序博客网 时间:2024/04/29 11:56

 

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

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();
    }

}

 

 

 

原创粉丝点击