Java多線程

ThreadDemo.java

public class ThreadDemo
{
    public static void main(String []args)
    {
        //Thread tt = new TestThread();
        //tt.setDaemon(true);
        /*Thread tt = new Thread(new TestThread());
        tt.start();//.run();
        int index=0;
        while(true)
        {
            if(index++ == 100)
                try{tt.join(10000);}catch(Exception e){}
            System.out.println("main():"+Thread.currentThread().getName());
           
           
        }*/
        /*new TestThread().start();
        new TestThread().start();
        new TestThread().start();
        new TestThread().start();*/
        TestThread tt = new TestThread();
        /*tt.start();
        tt.start();
        tt.start();
        tt.start();*/
        new Thread(tt).start();  //並不是馬上執行,只是就緒狀態;繼續main線程的執行。
        try{Thread.sleep(1);}catch(Exception e){}
        tt.str = new String("method");
        new Thread(tt).start();
        //new Thread(tt).start();
        //new Thread(tt).start();
    }
}
class TestThread implements Runnable //extends Thread
{
    int tickets = 100;
    String str = new String("");
    public void run()
    {
        if(str.equals("method"))
        {
            while(true)
            {
                sale();
            }
        }
        else
        {
            while(true)
            {
                //System.out.println("run():"+Thread.currentThread().getName());
                synchronized(str)       //同步代碼塊  str是同步對象
                {
                    if(tickets > 0)
                    {
                        try{Thread.sleep(10);}catch(Exception e){}
                        System.out.print("同步代碼塊():");
                        synchronized(this){}
                        System.out.println(Thread.currentThread().getName()+" is saling ticket "+tickets--);
                    }
                }
            }
        }

    }
    public synchronized void sale()    //同步函數
    {
        if(tickets > 0)
        {
            try{Thread.sleep(10);}catch(Exception e){}
            System.out.print("sale函數():");
            synchronized(str){}
            System.out.println(Thread.currentThread().getName()+" is saling ticket "+tickets--);
        }
    }
}

 

生產者與消費者問題

停止線程的方式

class Producer implements Runnable
{
    Q q;
    public Producer(Q q)
    {
        this.q = q;
    }
    public void run()
    {
        int i = 0;
        while(true)
        {
            /*synchronized(q)
            {
                if(q.bFull)
                    try{q.wait();}catch(Exception e){}
                if(i == 0)
                {
                    q.name = "zhangsan";
                    try{Thread.sleep(1);}catch(Exception e){}
                    q.sex = "male";
                }
                else
                {
                    q.name = "lisi";
                    q.sex = "female";
                }
                q.bFull=true;
                q.notify();
            }*/
            if(i == 0)
            {
                q.put("zhangsan", "male");
            }
            else
            {
                q.put("lisi", "female");
            }
            i = (i+1)%2;

        }
    }
    }

class Consumer implements Runnable
{
    Q q;
    public Consumer(Q q)
    {
        this.q = q;
    }
    public void run()
    {
        while(true)
        {
            /*synchronized(q)//解決線程同步問題--代碼塊
            {
                if(!q.bFull)
                    try{q.wait();}catch(Exception e){}//解決線程通信問題--等待
                System.out.print(q.name);
                System.out.println(":"+q.sex);
                q.bFull=false;
                q.notify();//解決線程通信問題--呼喚

            }*/
            q.get();

        }
    }
    }

class Q//這樣的類叫做線程安全的類,可以用於多線程操作。
{
    private String name="unknown";
    private String sex="unknown";
    private boolean bFull = false;
    public synchronized void put(String name,String sex)//解決線程同步問題--同步函數,對象爲鎖旗標
    {
        if(this.bFull)
            try{this.wait();}catch(Exception e){}//解決線程通信問題--等待
        this.name = name;
        try{Thread.sleep(1);}catch(Exception e){}
        this.sex = sex;
        this.bFull = true;
        this.notify();//解決線程通信問題--呼喚
    }
    public synchronized void get()
    {
        if(!this.bFull)
            try{this.wait();}catch(Exception e){}//解決線程通信問題--等待
        System.out.print(this.name);
        System.out.println(":"+this.sex);
        this.bFull = false;
        this.notify();//解決線程通信問題--呼喚
    }
    }

class ThreadCommunation
{
    public static void main(String []args)
    {
        /*Q q = new Q();
        new Thread(new Producer(q)).start();
        new Thread(new Consumer(q)).start();*/
        ThreadTest tt = new ThreadTest();
        new Thread(tt).start();
        for(int i=0;i<100;i++)
        {
            if(i==50)
            {
                tt.stopMe();
            }
            System.out.println("main() is running.");
        }
    }
    }
class ThreadTest implements Runnable
{
    boolean bFlag = false;
    public void stopMe()
    {
        bFlag = true;
    }
    public void run()
    {
        while(!bFlag)
        {
            System.out.println(Thread.currentThread().getName()+"is running.");
        }
    }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章