java吧 关注:1,301,396贴子:12,848,622

回复:【分享】有什么多线程的问题,拿出来溜溜,没准我会

只看楼主收藏回复



--------------------------魔兽老了还是我们都已长大了。。。如今我们这样子各奔前程的拼搏着,那段燃烧的岁月是否还记得?


IP属地:广西本楼含有高级字体16楼2014-03-20 10:12
收起回复
    这算是亮点吧~


    17楼2014-03-20 10:17
    收起回复
      2026-04-14 18:40:38
      广告
      不感兴趣
      开通SVIP免广告
      表示都不会


      来自Android客户端18楼2014-03-20 10:27
      收起回复
        解决了一记产品的大改造,心情很舒坦


        IP属地:北京19楼2014-03-20 18:15
        回复
          能保证运行效率吗


          IP属地:江苏20楼2014-03-20 18:21
          收起回复
            多线程什么的不太了解, 平时做项目也很少用到。。


            IP属地:上海21楼2014-03-20 20:19
            收起回复
              大神求帮忙,为何只有Thread-0,。。。搞不懂。。。上码!!!
              public class TicketsTest {
              public static void main(String[] args) throws InterruptedException {
              TicketsThread tt = new TicketsThread();
              new Thread(tt).start(); //0
              new Thread(tt).start(); //1
              Thread.sleep(10);
              tt.s = "method";
              System.out.println(tt.s);
              new Thread(tt).start(); //2
              new Thread(tt).start(); //3
              }
              }
              class TicketsThread implements Runnable {
              private int num=100;
              public String s="";
              public void run() {
              if(s.equals("method"))
              {
              while(true) {
              sal();
              }
              }
              else {
              while(true){
              synchronized(this) {
              if(num>0) {
              try {
              Thread.sleep(100);
              } catch (InterruptedException e) {
              e.printStackTrace();
              }
              System.out.println(Thread.currentThread().getName()+" " +num-- + "tickets ");
              }
              }
              }
              }
              }
              private synchronized void sal() {
              if(num>0){
              try {
              Thread.sleep(100);
              } catch (InterruptedException e) {
              e.printStackTrace();
              }
              System.out.println(Thread.currentThread().getName()+" " +num-- + "tickets ");
              }
              }
              }


              IP属地:陕西22楼2014-03-20 20:23
              收起回复
                public class TicketsTest {
                public static void main(String[] args) throws InterruptedException {
                TicketsThread tt = new TicketsThread();
                new Thread(tt).start(); //0
                new Thread(tt).start(); //1
                Thread.sleep(10);
                tt.s = "method";
                System.out.println(tt.s);
                new Thread(tt).start(); //2
                new Thread(tt).start(); //3
                }
                }
                class TicketsThread implements Runnable {
                private int num=100;
                public String s="";
                public void run() {
                if(s.equals("method"))
                {
                while(true) {
                sal();
                }
                }
                else {
                while(true){
                synchronized(this) {
                if(num>0) {
                try {
                Thread.sleep(100);
                } catch (InterruptedException e) {
                e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+" " +num-- + "tickets ");
                }
                }
                }
                }
                }
                private synchronized void sal() {
                if(num>0){
                try {
                Thread.sleep(100);
                } catch (InterruptedException e) {
                e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+" " +num-- + "tickets ");
                }
                }
                }


                IP属地:陕西23楼2014-03-20 20:26
                回复
                  2026-04-14 18:34:38
                  广告
                  不感兴趣
                  开通SVIP免广告
                  跟进


                  来自Android客户端25楼2014-03-20 20:46
                  回复
                    题目 三个同步线程 打印输出
                    1aA…26zZ


                    IP属地:浙江来自Android客户端26楼2014-03-21 09:24
                    收起回复

                      ——来自 爱贴吧 Windows Phone 客户端


                      IP属地:北京来自iPhone客户端27楼2014-03-21 09:52
                      收起回复


                        IP属地:广东28楼2014-03-21 10:39
                        收起回复
                          看不懂


                          IP属地:广东来自iPhone客户端29楼2014-03-21 10:41
                          收起回复
                            j8里水经验略难啊


                            IP属地:北京30楼2014-03-21 12:57
                            回复