一个线程例子
本文最后更新于 2016 天前,其中的信息可能已经有所发展或是发生改变。

emmm…

代码如下

import java.util.ArrayList;
import java.util.List;

public class Main {

    private static List<Integer> list = new ArrayList<>();

    public static void main(String[] args) {
        Thread thread1 = new Thread(new TestRunnable(list));
        thread1.start();

        TestThread thread2 = new TestThread(list);
        thread2.run();

    }

   static class TestRunnable implements Runnable{

       private  List<Integer> list;

       public TestRunnable(List<Integer> list) {
           this.list = list;
       }

       @Override
        public void run() {
            Thread.currentThread().setName("Runnable线程");
            for(int i=0;i<200;i++){
                TestThreadAddList testThreadAddList = new TestThreadAddList(i,list);
                testThreadAddList.run();
               // System.out.println(Thread.currentThread().getName()+"第"+i+"个");
               // System.out.println(Thread.currentThread().getName()+"list大小"+list.size());
            }

        }
    }

    static class TestThread extends Thread{
        private  List<Integer> list;

        public TestThread(List<Integer> list) {
            this.list = list;
        }

        @Override
        public void run() {
            Thread.currentThread().setName("Thread线程");
            for(int i=0;i<200;i++){
                TestThreadAddList testThreadAddList = new TestThreadAddList(i,list);
                testThreadAddList.run();
               // System.out.println(Thread.currentThread().getName()+"list大小"+list.size());
                // System.out.println(Thread.currentThread().getName()+"第"+i+"个");
            }
        }
    }

    static class TestThreadAddList extends Thread{
        private  int num;
        private  List<Integer> list;

        public TestThreadAddList( int num, List<Integer> list) {
            this.num = num;
            this.list = list;
        }

        @Override
        public void run() {
            synchronized (TestThreadAddList.class){
              list.add(num);
                System.out.println(Thread.currentThread().getName()+"list大小"+list.size());
            }
        }
    }

}

评论

  1. Windows Chrome 68.0.3440.84
    6 年前
    2018-8-08 0:09:21

    emmm…

    看不懂…

  2. Windows Chrome 55.0.2883.87
    6 年前
    2018-8-08 10:28:48

    一起加油吧!【最近在备省赛,看上去简单,但是一个个细节真的头疼死了

  3. xrabbit
    Windows Chrome 68.0.3440.84
    6 年前
    2018-8-09 11:47:52

    加油

  4. Windows Chrome 67.0.3396.62
    6 年前
    2018-8-19 21:33:48

    看来是技术性博客哈!

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇