Java线程问题总结

public class ThreadTest {
    public static void main(String[] args){
    //    System.out.println("HelloWorld");
        Counter c = new Counter();
       Thread n1 = new Thread(new Run(c));
       n1.setName("n1");
        Thread n2 = new Thread(new Run(c));
        n2.setName("n2");

        n1.start();
        n2.start();
       /* Thread n3 = new Thread(new Run(c));
        n3.setName("n3");
        Thread n4 = new Thread(new Run(c));
        n4.setName("n4");
        Thread n5 = new Thread(new Run(c));
        n5.setName("n5");
*/
        try{
            Thread.currentThread().sleep(1000);
        }catch(InterruptedException e){}


/*
        Thread st = new SonThread("SonThread start");
        st.start();

        System.out.println(Thread.currentThread()); //拿到当前的线程

        try{
            Thread.sleep(1000);
        //    st.join();  //线程合并
        }catch(InterruptedException e){
            e.printStackTrace();
        }



        st.interrupt();
*/


    }
}

class Run implements Runnable{
    String printText;
    Counter c;

    Run(String printText){
       this.printText = printText;
    }

    Run(Counter c){
        this.c = c;
    }

    @Override
    public void run() {
        if(Thread.currentThread().getName().equals("n2")){
            c.test2();
        }else{
            c.test();
        }


    }

}

class SonThread extends Thread{
    String printText;

    SonThread(String printText){
        this.printText = printText;
    }
    @Override
    public void run() {
        yield();

        System.out.println(printText);
        System.out.println(isAlive());
        System.out.println(getPriority());


        try{
            sleep(5000);
            System.out.println("5秒等待完毕!");
        }catch(InterruptedException e){
            e.printStackTrace();
            return;
        }


    }

    @Override
    public synchronized void start() {
        super.start();
        System.out.println("线程启动后我还要干一点事情……");
    }
}

class Counter{
    private int num = 0;

    public void add(){
        synchronized(this) {
            num++;
            System.out.println("当前线程名:" + Thread.currentThread().getName() + " num当前值:" + num);

            try {
                Thread.currentThread().sleep(5000);
            } catch (InterruptedException e) {
            }
        }
    }
    public void test(){
        synchronized (this){
            System.out.println("同步调试");

            try {
                Thread.currentThread().sleep(5000);
            } catch (InterruptedException e) { }
        }
    }

    public void test2(){
        synchronized (this) {
            System.out.println("同步调试222");
        }
    }
}

/*
*   synchronized 对象锁是此对象所拥有的一把锁,其他同步代码都会受到这一把锁的影响
*   线程开启的两种方式 一种是实现Runnable接口 另一种则是继承Thread线程
*   synchronized 只锁定被圈定的代码段 其他未被圈定的代码不受影响
*   多线程同时调用synchronized线程锁的代码,会被放入等待锁池中
*
* */
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和...
    Java小辰阅读 10,178评论 5 209
  • 前言 个人认为,学习,内容越多、越杂的知识,越需要进行深刻的总结,这样才能记忆深刻,将知识变成自己的。这篇文章主要...
    raysonfang阅读 1,279评论 0 1
  • 前言 个人认为,学习,内容越多、越杂的知识,越需要进行深刻的总结,这样才能记忆深刻,将知识变成自己的。这篇文章主要...
    尧淳阅读 3,923评论 0 17
  • 1、多线程有什么用? 一个可能在很多人看来很扯淡的一个问题:我会用多线程就好了,还管它有什么用?。所谓”知其然知其...
    小宇java阅读 1,783评论 1 1
  • 前言 这篇文章主要是对多线程的问题进行总结的,因此罗列了40个多线程的问题。 这些多线程的问题,有些来源于各大网站...
    Java高级架构狮阅读 4,413评论 0 12

友情链接更多精彩内容