⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j_synchronization.java

📁 一个十分好的java基础学习的课件
💻 JAVA
字号:
// ////////////////////////////////////////////////////////
// 
// J_Synchronization.java
// 

// ////////////////////////////////////////////////////////

class J_Experiment
{
    private int m_temperature, m_pressure;
    private boolean m_ready=false;
    
    public synchronized void mb_update(int t, int p)
    {
        while (m_ready)
        {
            try
            {
                wait( );
            }
            catch(Exception e)
            {
            }
        }
        m_temperature = t;
        m_pressure    = p;
        System.out.println("Update: temperature=" + t + ", pressure=" + p + ".");
        m_ready=true;
        notify( );
    } // End of method: mb_update
    
    public synchronized void mb_analyze( )
    {
        while (!m_ready)
        {
            try
            {
                wait( );
            }
            catch(Exception e)
            {
            }
        }
        int t= m_temperature;
        int p= m_pressure;
        System.out.println("Get: temperature=" + t + ", pressure=" + p + ".");
        m_ready=false;
        notify( );
    } // End of method: mb_analyze
} // End of class: J_Experiment

class J_Assistant extends Thread
{
    J_Experiment m_data;
    
    public J_Assistant(J_Experiment d)
    {
        m_data= d;
    } // End of constructor

    public void run( )
    {
        int i, j, k;
        for(k= 0; k< 4; k++)
        {
            i= (int)(Math.random( ) * 1000);
            j= (int)(Math.random( ) * 1000);
            m_data.mb_update(i, j);
        }
    } // End of method: run
} // End of class: J_Assistant

class J_Analyst extends Thread
{
    J_Experiment m_data;
    
    public J_Analyst(J_Experiment d)
    {
        m_data= d;
    } // End of constructor
    
    public void run( )
    {
        int k;
        for(k= 0; k< 4; k++)
        {
            m_data.mb_analyze( );
        }
    } // End of method: run
} // End of class: J_Analyst

public class J_Synchronization
{
    
    public static void main( String args[] )
    { 
        J_Experiment data= new J_Experiment( );
        J_Assistant threadA = new J_Assistant(data);
        J_Analyst   threadB = new J_Analyst(data);
        threadA.start( );
        threadB.start( );
    } // End of method: main

}  // End of class: J_Synchronization

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -