java something

          不要以為......很遙遠
          隨筆 - 23, 文章 - 1, 評論 - 2, 引用 - 0
          數(shù)據(jù)加載中……

          2011年9月2日

          Activity生命周期

          現(xiàn)有兩個Activity:  Activity1,Activity2

          先啟動Activity1運行順序為: Activity1 onCreate -> Activity1 onStart -> Activity1 onResume
          用Intent從Activity1跳到Activity2運行順序 : 
          Activity1 onPause -> Activity2 onCreate -> Activity2 onStart -> Activity2 onResume ->Activity1 onStop -> Activity1  onDestroy
          退出應(yīng)用程序: Activity2 onResume ->Activity2 onStop -> Activity2  onDestroy

          posted @ 2011-09-02 17:48 Jamie 閱讀(229) | 評論 (0)編輯 收藏

          控制3個線程運行順序的Demo

          本程序可以控制3個線程按順序執(zhí)行, 代碼如下:

          public class Test3 {

           public static void main(String[] args) throws IOException {
            final Test obj = new Test();
            
            new Thread()
            {
             public void run()
             {
              obj.m1();
             }
            }.start();
            new Thread()
            {
             public void run()
             {
              obj.m2();
             }
            }.start();
            new Thread()
            {
             public void run()
             {
              obj.m3();
             }
            }.start();
            
           }

          }

          class Test
          {
           static int count;
           volatile int target = 1;
           synchronized void m1()
           { 
             for (int i = 0; i < 10; i++)
             {
              while (target == 2 || target == 3)
              {
               try {
                wait();
               } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
               }
              }
              System.out.println("m1() =" + i);
              target = 2;
              notifyAll();
             }
           }
           
           synchronized void m2()
           {
            for (int i = 0; i < 10; i++)
            {
             while (target == 1 || target == 3)
             {
              try {
               wait();
              } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
              }
             }
             System.out.println("m2() =" + i);
             target = 3;
             notifyAll();
            }
           }
           
           synchronized void m3()
           {
            for (int i = 0; i < 10; i++)
            {
             while (target == 1 || target == 2)
             {
              try {
               wait();
              } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
              }
             }
             System.out.println("m3() =" + i);
             target = 1;
             notifyAll();
            }
           }
          }

          posted @ 2011-09-02 02:27 Jamie 閱讀(1777) | 評論 (2)編輯 收藏

          線程的同步與共享

               摘要: 線程的同步與共享 前面程序中的線程都是獨立的、異步執(zhí)行的線程。但在很多情況下,多個線程需要共享數(shù)據(jù)資源,這就涉及到線程的同步與資源共享的問題。 1 資源沖突 下面的例子說明,多個線程共享資源,如果不加以控制可能會產(chǎn)生沖突。 程序CounterTest.java   Code highlighting produced by Actipro CodeHighlight...  閱讀全文

          posted @ 2011-09-02 01:38 Jamie 閱讀(484) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 九龙坡区| 云梦县| 宜兴市| 罗田县| 平顶山市| 沈阳市| 南靖县| 巴南区| 阿坝县| 丹寨县| 成都市| 武冈市| 茶陵县| 五河县| 高碑店市| 西吉县| 遵义市| 桂林市| 崇明县| 呼图壁县| 兴安县| 梅河口市| 左权县| 萨嘎县| 辽宁省| 莫力| 获嘉县| 东乌| 东宁县| 绵阳市| 西乡县| 澎湖县| 涡阳县| 民县| 兴山县| 永城市| 余干县| 肥东县| 射阳县| 秦皇岛市| 和政县|