JAVA多线程基础实验编码及学习笔记总结

关于多线程的基础知识和相关概念,以及编程时可能会出现的问题参见我之前的两个博客
Java多线程和操作系统多线程关系
操作系统中进程和线程的概念理解

多线程基础实验

import Threads.*;
public class Main {
    public static void main(String[] args) {

        /*
         * 
         * 简单的Thread类使用
         * sleep()使得当前线程休眠 n ms
         * start()使得该线程开始工作(并不是执行状态
         * run() 为Thread类的内置函数,线程运行时执行的方法(注意区分start()和run())
         * 
         */

        System.out.println("Main Start.");
        ThreadFir threadFir = new ThreadFir();
        threadFir.start();

        //      
        try {
            System.out.println("Main Sleep.");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Main End.");



    }
}

并发编程实验

package Test;
import Things.Count;
import Threads.ThreadFir;
import Threads.ThreadFour;
import Threads.ThreadSec;
import Threads.ThreadThir;


public class ConcurrentTest {
    public static void main(String [] args){

        /*并发编程实验(一)
         * 
         * 同时开始两个线程会发现两个线程的调度顺序
         * 发生不同的变化
         */

//      ThreadFir threadFir = new ThreadFir();
//      ThreadSec threadSec = new ThreadSec();
//      threadFir.start();
//      threadSec.start();
//      

        /*
         * 并发编程实验(二)
         * 
         * 使用FOR循环进行1000个编程对Count的加减操作
         * 结果应该为1000
         * 但是由于并发冲突会发现结果并不等于1000
         * thread3 为并发冲突
         * thread4为同步锁synchronized
         * 若是现象不明显可以是适当增大 循环次数。(i7-4710 cpu在循环次数为50000的情况下出现值为1的差异)
         */
        Count mCount = new Count(0);
        Count cCount = new Count(0);
        for(int i = 0 ; i < 1000 ; i++){

            ThreadThir threadThir = new ThreadThir(mCount);
            threadThir.start();

            ThreadFour threadFour = new ThreadFour(cCount);
            threadFour.start();
        }
        System.out.println("Thread3: " + mCount.count);
        System.out.println("Thread4: " + cCount.count);


    }
}

JOIN方法实验

package Test;

import Threads.ThreadSix;

public class JoinTest {
    public static void main(String [] args){

        /*
         * join()方法
         * JDK是这样说的:join public final void join(long millis)
         * throws InterruptedException Waits at most millis milliseconds 
         * for this thread to die. A timeout of 0 means to wait forever.
         * 字面意思是等待一段时间直到这个线程死亡
         */
        System.out.println("main running.");
        ThreadSix t = new ThreadSix("t");
        ThreadSix s = new ThreadSix("s"); 

        t.start();
        s.start();

        try {
            t.join(); // 这里main线程会等到t线程执行完后再执行而不会等s执行完
            //所以多次运行可以看到s会有时候最后结束, 而t总在main之前结束
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("main finish.");
    }
}

SYNCHRONIZED同步锁实验

package Test;

import Things.Count;
import Threads.ThreadFour;

public class SynchronizedTest {
    public static void main(String [] args){

        /*
         * 同步锁实验
         * 将临界资源Count加上锁
         * 在进行修改会发现值稳定在1000
         * 不会出现并发冲突
         */

        Count mCount = new Count(0);

        for(int i = 0 ; i < 1000 ; i++){
            ThreadFour threadFour = new ThreadFour(mCount);
            threadFour.start();
        }

        System.out.println("Thread4: " + mCount.count);
    }
}

生产者消费者模型实验

package Test;

import Things.Count;
import Threads.ConsumeThread;
import Threads.ProductThread;

public class ProAndConsume {

    public static void main(String[] args) {

        /*
         * 生产者消费者模型
         * 采用管程机制实现同步
         */
        Count mCount = new Count(1);

        for(int i = 1 ; i <= 10 ; i++) {

            ProductThread pro = new ProductThread(mCount,i);
            ConsumeThread con = new ConsumeThread(mCount,i);

            pro.start();
            con.start();
        }
    }
}

消费者线程

package Threads;

import Things.Count;

public class ConsumeThread extends Thread {

    private Count mCount;
    private int sum;

    public ConsumeThread(Count c, int s) {
        this.mCount = c;
        this.sum = s;
    }

    public void run() {

        synchronized (mCount) {

            while (mCount.count < sum) {

                System.out.println("count not enough ; waiting pro");

                try {
                    mCount.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            for (int i = 0; i < sum; i++)
                mCount.changeCountSYN(false);
            System.out.println("Con: " + sum);

            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            mCount.notify();
        }
    }

}

生产者线程

package Threads;

import Things.Count;

public class ProductThread extends Thread {

    private Count mCount;
    private int sum;

    public ProductThread(Count c, int s) {
        this.mCount = c;
        this.sum = s;
    }

    public void run() {

        synchronized (mCount) {
            while (mCount.count  > 3) {

                System.out.println("count > 3 : waiting consume");

                try {
                    mCount.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

            for (int i = 0; i < sum; i++)
                mCount.changeCountSYN(true);
            System.out.println("Pro: " + sum);

            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            mCount.notify();
        }
    }
}

附完整版源码下载
CSDN项目

你可能感兴趣的:(Java编程)