少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

          #

          雖然natural join(自然連接)實際上的用的比較少,但實際上這個連接是非常有用的,若能經(jīng)常使用一下,實際上是非常方便的。

          自然連接是在兩張表中尋找那些數(shù)據(jù)類型和列名都相同的字段,然后自動地將他們連接起來,并返回所有符合條件按的結(jié)果。

          來看一下自然連接的例子。

          Select emp.ename,dept.dname

          From emp natural join dept;

          這里我們并沒有指定連接的條件,實際上oracle為我們自作主張的將,emp中的deptno和dept中的deptno做了連接。

          也就是實際上相當(dāng)于

          Select emp.ename,dept.dname

          From emp join dept on emp.deptno = dept.deptno;

          因為這兩張表的這兩個字段deptno的類型個名稱完全相同。所以使用natural join時被自然的連接在一起了。

          另外:

          1.如果做自然連接的兩個表的有多個字段都滿足有相同名稱個類型,那么他們會被作為自然連接的條件。

          2.如果自然連接的兩個表僅是字段名稱相同,但數(shù)據(jù)類型不同,那么將會返回一個錯誤。

          3.由于oracle中可以進行這種非常簡單的natural join,我們在設(shè)計表時,應(yīng)該盡量在不同表中具有相同含義的字段使用相同的名字和數(shù)據(jù)類型。以方便以后使用natural join

          最后我們在前面舉的例子都得到以下的結(jié)果:

          SQL> Select emp.ename,dept.dname

          2 From emp natural join dept;

          ENAME DNAME

          ——————– —————-

          SMITH RESEARCH

          ALLEN SALES

          WARD SALES

          JONES RESEARCH

          MARTIN SALES

          BLAKE SALES

          CLARK ACCOUNTING

          SCOTT RESEARCH

          KING ACCOUNTING

          TURNER SALES

          ADAMS RESEARCH

          JAMES SALES

          FORD RESEARCH

          MILLER ACCOUNTING

          posted @ 2012-12-04 23:22 abin 閱讀(11609) | 評論 (1)編輯 收藏

          SQL> create table lee(id number,name varchar2(100),score number,constraint pk_lee primary key(id));
           
          Table created

           SQL> desc lee
          Name  Type          Nullable Default Comments
          ----- ------------- -------- ------- --------
          ID    NUMBER                                 
          NAME  VARCHAR2(100) Y                        
          SCORE NUMBER        Y                        
           
          SQL> alter table lee add createtime date;
           
          Table altered
           
          SQL> desc lee
          Name       Type          Nullable Default Comments
          ---------- ------------- -------- ------- --------
          ID         NUMBER                                 
          NAME       VARCHAR2(100) Y                        
          SCORE      NUMBER        Y                        
          CREATETIME DATE          Y                        
           
          SQL> alter table lee modify createtime nvarchar2(100);
           
          Table altered
           
          SQL> desc lee
          Name       Type           Nullable Default Comments
          ---------- -------------- -------- ------- --------
          ID         NUMBER                                  
          NAME       VARCHAR2(100)  Y                        
          SCORE      NUMBER         Y                        
          CREATETIME NVARCHAR2(100) Y                        
           
          SQL> alter table lee rename column createtime to mytime;
           
          Table altered
           
          SQL> desc lee
          Name   Type           Nullable Default Comments
          ------ -------------- -------- ------- --------
          ID     NUMBER                                  
          NAME   VARCHAR2(100)  Y                        
          SCORE  NUMBER         Y                        
          MYTIME NVARCHAR2(100) Y                        
           
          SQL> alter table lee modify mytime date;
           
          Table altered
           
          SQL> desc lee
          Name   Type          Nullable Default Comments
          ------ ------------- -------- ------- --------
          ID     NUMBER                                 
          NAME   VARCHAR2(100) Y                        
          SCORE  NUMBER        Y                        
          MYTIME DATE          Y                        
           
          SQL> desc lee
          Name   Type          Nullable Default Comments
          ------ ------------- -------- ------- --------
          ID     NUMBER                                 
          NAME   VARCHAR2(100) Y                        
          SCORE  NUMBER        Y                        
          MYTIME DATE          Y                         
                            
           
          SQL> alter table lee rename column mytime to createtime;
           
          Table altered
           
          SQL> desc lee
          Name       Type          Nullable Default Comments
          ---------- ------------- -------- ------- --------
          ID         NUMBER                                 
          NAME       VARCHAR2(100) Y                        
          SCORE      NUMBER        Y                        
          CREATETIME DATE          Y                         
           
          SQL> alter table lee drop column createtime;
           
          Table altered
           
          SQL> desc lee
          Name  Type          Nullable Default Comments
          ----- ------------- -------- ------- --------
          ID    NUMBER                                 
          NAME  VARCHAR2(100) Y                        
          SCORE NUMBER        Y                        
           
          SQL>
          posted @ 2012-12-03 10:51 abin 閱讀(578) | 評論 (0)編輯 收藏

          //這里是測試UserDao userDao=EasyMock.createMock(UserDao.class);這種形式的:

          package com.abin.lee.mock;

          public class User {
           private int id;
           private String userName;
           private String passWord;
           public int getId() {
            return id;
           }
           public void setId(int id) {
            this.id = id;
           }
           public String getUserName() {
            return userName;
           }
           public void setUserName(String userName) {
            this.userName = userName;
           }
           public String getPassWord() {
            return passWord;
           }
           public void setPassWord(String passWord) {
            this.passWord = passWord;
           }
          }




          package com.abin.lee.mock;

          public interface UserDao {
           User query(String id);
          }




          package com.abin.lee.mock;

          public class UserDaoImpl implements UserDao{

           public User query(String id) {
            User user=null;
            if(id.equals("1")){
             user=new User();
             user.setId(1);
             user.setUserName("abin1");
             user.setPassWord("varyall1");
            }
            if(id.equals("2")){
             user=new User();
             user.setId(2);
             user.setUserName("abin2");
             user.setPassWord("varyall2");
            }
            return user;
           }

          }





          package com.abin.lee.mock;

          public interface UserService {
           User query(String id);
          }




          package com.abin.lee.mock;

          public class UserServiceImpl implements UserService{
           private UserDao userDao;
           public User query(String id){
            return this.userDao.query(id);
           }
           
           public UserDao getUserDao() {
            return userDao;
           }

           public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
           }
           
          }





          測試代碼:

          package com.abin.lee.mock;

          import org.easymock.EasyMock;
          import org.junit.Assert;
          import org.junit.Test;

          public class UserMock {
           @Test
           public void test(){
            User expectedUser=new User();
            expectedUser.setId(1);
            expectedUser.setUserName("abin1");
            expectedUser.setPassWord("varyall1");
            UserDao userDao=EasyMock.createMock(UserDao.class);
            EasyMock.expect(userDao.query("1")).andReturn(expectedUser);
            EasyMock.replay(userDao);
            UserServiceImpl service=new UserServiceImpl();
            service.setUserDao(userDao);
            User user=service.query("1");
            Assert.assertNotNull(user);
            Assert.assertEquals(1, user.getId());
            Assert.assertEquals("abin1", user.getUserName());
            Assert.assertEquals("varyall1", user.getPassWord());
            EasyMock.verify(userDao);
           }
          }





          測試方法2:

          package com.abin.lee.mock;

          import org.easymock.EasyMock;
          import org.easymock.IMocksControl;
          import org.junit.Assert;
          import org.junit.Test;

          public class UsersMock {
           @Test
           public void test(){
            User expectedUser=new User();
            expectedUser.setId(2);
            expectedUser.setUserName("abin2");
            expectedUser.setPassWord("varyall2");
            IMocksControl mock=EasyMock.createNiceControl();
            UserDao userDao=mock.createMock(UserDao.class);
            EasyMock.expect(userDao.query("2")).andReturn(expectedUser);
            mock.replay();
            UserServiceImpl service=new UserServiceImpl();
            service.setUserDao(userDao);
            User user=service.query("2");
            Assert.assertNotNull(user);
            Assert.assertEquals(2, user.getId());
            Assert.assertEquals("abin2", user.getUserName());
            Assert.assertEquals("varyall2", user.getPassWord());
            mock.verify();
            mock.resetToNice();
            
           }

          }


          posted @ 2012-11-27 22:40 abin 閱讀(542) | 評論 (0)編輯 收藏

          對于抽象類和接口,我個人覺得,一般性的接口,都可以用這兩者,
          1、接口,接口實現(xiàn)類,
          2、普通類繼承抽象類

          我想問下,什么場合用接口好點,什么場合用抽象類好點
          接口類似一個協(xié)議,一般只作定義
          面向?qū)ο蟮恼Z意是完全不同的
          一種是實現(xiàn),一種是個別化
          看uml 就知道了
          這個都用過,接口用的是最多的,但是能不能給講個語境,比如這里用抽象類就比接口好點
          目前,有觀點就是,使用接口 解耦
          不是這樣地,模式里用的接口多,但有些模式,如模版方法模式,用到抽像
          看具體的使用
          恩恩,總覺得迷迷糊糊的
          之前我公司他們寫呼叫系統(tǒng)的時候,大量的使用了抽象類
          抽象類可以定義方法的內(nèi)容,具體實現(xiàn)留給子類實現(xiàn)
          抽象的目的是個別化,就是各個子類都有自己的特性
          雖然都繼承父類,但有些方法需要重新,或新增,就是實現(xiàn)了子類的特殊性
          接口不一樣,接口只是單獨的 realize
          posted @ 2012-11-22 17:01 abin 閱讀(416) | 評論 (0)編輯 收藏

          sql 單表/多表查詢?nèi)コ貜?fù)記錄

          單表distinct

          多表group by

          group by 必須放在 order by 和 limit之前,不然會報錯

          ************************************************************************************

          1、查找表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個字段(peopleId)來判斷

          select * from people
          where peopleId in (select  peopleId  from  people  group  by  peopleId  having  count(peopleId) > 1)

          2、刪除表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個字段(peopleId)來判斷,只留有rowid最小的記錄
          delete from people
          where peopleId  in (select  peopleId  from people  group  by  peopleId   having  count(peopleId) > 1)
          and rowid not in (select min(rowid) from  people  group by peopleId  having count(peopleId )>1)

          3、查找表中多余的重復(fù)記錄(多個字段)
          select * from vitae a
          where (a.peopleId,a.seq) in  (select peopleId,seq from vitae group by peopleId,seq  having count(*) > 1)

          4、刪除表中多余的重復(fù)記錄(多個字段),只留有rowid最小的記錄
          delete from vitae a
          where (a.peopleId,a.seq) in  (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
          and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)


          5、查找表中多余的重復(fù)記錄(多個字段),不包含rowid最小的記錄
          select * from vitae a
          where (a.peopleId,a.seq) in  (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
          and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

          (二)
          比方說
          在A表中存在一個字段“name”,
          而且不同記錄之間的“name”值有可能會相同,
          現(xiàn)在就是需要查詢出在該表中的各記錄之間,“name”值存在重復(fù)的項;
          Select Name,Count(*) From A Group By Name Having Count(*) > 1

          如果還查性別也相同大則如下:
          Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1

          (三)
          方法一

          declare @max integer,@id integer

          declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 1

          open cur_rows

          fetch cur_rows into @id,@max

          while @@fetch_status=0

          begin

          select @max = @max -1

          set rowcount @max

          delete from 表名 where 主字段 = @id

          fetch cur_rows into @id,@max
          end

          close cur_rows

          set rowcount 0

          方法二

          "重復(fù)記錄"有兩個意義上的重復(fù)記錄,一是完全重復(fù)的記錄,也即所有字段均重復(fù)的記錄,二是部分關(guān)鍵字段重復(fù)的記錄,比如Name字段重復(fù),而其他字段不一定重復(fù)或都重復(fù)可以忽略。

          1、對于第一種重復(fù),比較容易解決,使用

          select distinct * from tableName

          就可以得到無重復(fù)記錄的結(jié)果集。

          如果該表需要刪除重復(fù)的記錄(重復(fù)記錄保留1條),可以按以下方法刪除

          select distinct * into #Tmp from tableName

          drop table tableName

          select * into tableName from #Tmp
          drop table #Tmp

          發(fā)生這種重復(fù)的原因是表設(shè)計不周產(chǎn)生的,增加唯一索引列即可解決。

          2、這類重復(fù)問題通常要求保留重復(fù)記錄中的第一條記錄,操作方法如下

          假設(shè)有重復(fù)的字段為Name,Address,要求得到這兩個字段唯一的結(jié)果集

          select identity(int,1,1) as autoID, * into #Tmp from tableName

          select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID

          select * from #Tmp where autoID in(select autoID from #tmp2)

          最后一個select即得到了Name,Address不重復(fù)的結(jié)果集(但多了一個autoID字段,實際寫時可以寫在select子句中省去此列)

          (四)
          查詢重復(fù)

          select * from tablename where id in (select id from tablename

          group by id

          having count(id) > 1

          )

          3、查找表中多余的重復(fù)記錄(多個字段)
          select * from vitae a
          where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

          運行會產(chǎn)生問題,where(a.peopleId,a.seq)這樣的寫發(fā)是通不過的!!!

          posted @ 2012-11-21 23:20 abin 閱讀(549) | 評論 (0)編輯 收藏

               摘要: 命令1:監(jiān)聽命令 nc -l -p port nc -l -p port > e:\log.dat nc -l -v -p port 參數(shù)解釋: -l:監(jiān)聽端口,監(jiān)聽入站信息 -p:后跟本地端口號 -v:顯示端口的信息,如果使用-vv的話,則會顯示端口更詳細的信息 提示:一般大家都愛用-vv nc -l...  閱讀全文
          posted @ 2012-11-21 14:57 abin 閱讀(2012) | 評論 (0)編輯 收藏

          package chn.lass.liu.collection;

          import java.util.HashMap;
          import java.util.Iterator;
          import java.util.Map;

          public class MapTest {
           public static Map<String,String> createMap(){
            Map<String,String> map=new HashMap<String, String>();
            map.put("a", "111");
            map.put("b", "222");
            return map;
           }
           public static void parseMap(Map<String,String> map){
            for(Iterator it=map.entrySet().iterator();it.hasNext();){
             Map.Entry entry=(Map.Entry)it.next();
             System.out.println("key="+entry.getKey());
             System.out.println("value="+entry.getValue());
             if("a".equals(entry.getKey())){
              it.remove();
             }
            }
            for(Iterator it=map.entrySet().iterator();it.hasNext();){
             Map.Entry entry=(Map.Entry)it.next();
             System.out.println("key1="+entry.getKey());
             System.out.println("value1="+entry.getValue());
            }
           }
           public static void main(String[] args) {
            Map<String,String> map=createMap();
            parseMap(map);
           }
          }





          package chn.lass.liu.collection;

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

          public class ListTest {
           public static List<String> createList(){
            List<String> list=new ArrayList<String>();
            for(int i=0;i<5;i++){
             list.add(""+i);
            }
            return list;
           }
           public static void ParseList(List<String> list){
            for(Iterator it=list.iterator();it.hasNext();){
             String its=(String)it.next();
             System.out.println("list is:"+its);
             it.remove();
            }
           }
           public static void main(String[] args) {
             List<String> list=createList();
             ParseList(list);
           }
          }






          package chn.lass.liu.collection;

          import java.util.Iterator;
          import java.util.Vector;

          public class VectorTest {
           public static Vector<String> createVector(){
            Vector<String> vector=new Vector<String>();
            vector.addElement("aa");
            vector.addElement("bb");
            return vector;
           }
           public static void parseVector(Vector<String> vector){
            for(Iterator it=vector.iterator();it.hasNext();){
             String its=(String)it.next();
             System.out.println("its is:"+its);
            }
           }
           public static void main(String[] args) {
            Vector<String> vector=createVector();
            parseVector(vector);
           }
          }





          package chn.lass.liu.collection;

          import java.util.HashSet;
          import java.util.Iterator;
          import java.util.Set;

          public class SetTest {
           public static Set<String> createSet(){
            Set<String> set=new HashSet<String>();
            set.add("a");
            set.add("b");
            return set;
           }
           public static void parseSet(Set<String> set){
            for(Iterator it=set.iterator();it.hasNext();){
             String its=(String)it.next();
             System.out.println("its is:"+its);
            }
           }
           public static void main(String[] args) {
            Set<String> set=createSet();
            parseSet(set);
           }
          }

           

          posted @ 2012-11-20 00:19 abin 閱讀(2047) | 評論 (0)編輯 收藏

           Netcat 或者叫 nc 是 Linux 下的一個用于調(diào)試和檢查網(wǎng)絡(luò)工具包。可用于創(chuàng)建 TCP/IP 連接,最大的用途就是用來處理 TCP/UDP 套接字。

            這里我們將通過一些實例來學(xué)習(xí) netcat 命令。

            1. 在服務(wù)器-客戶端架構(gòu)上使用 Netcat

            netcat 工具可運行于服務(wù)器模式,偵聽指定端口

          1
          $ nc -l 2389

           然后你可以使用客戶端模式來連接到 2389 端口:

          1
          $ nc localhost 2389

            現(xiàn)在如果你輸入一些文本,它將被發(fā)送到服務(wù)器端:

          1
          2
          $ nc localhost 2389
          HI, oschina

            在服務(wù)器的終端窗口將會顯示下面內(nèi)容:

          1
          2
          $ nc -l 2389
          HI, oschina

            2. 使用 Netcat 來傳輸文件

            netcat 工具還可用來傳輸文件,在客戶端,假設(shè)我們有一個 testfile 文件:

          1
          2
          $ cat testfile
          hello oschina

            而在服務(wù)器端有一個空文件名為 test

            然后我們使用如下命令來啟用服務(wù)器端:

          1
          $ nc -l 2389 > test

            緊接著運行客戶端:

          1
          cat testfile | nc localhost 2389

            然后你停止服務(wù)器端,你可以查看 test 內(nèi)容就是剛才客戶端傳過來的 testfile 文件的內(nèi)容:

          1
          2
          $ cat test
          hello oschina

            3. Netcat 支持超時控制

            多數(shù)情況我們不希望連接一直保持,那么我們可以使用 -w 參數(shù)來指定連接的空閑超時時間,該參數(shù)緊接一個數(shù)值,代表秒數(shù),如果連接超過指定時間則連接會被終止。

            服務(wù)器:

          1
          nc -l 2389

            客戶端:

          1
          $ nc -w 10 localhost 2389

            該連接將在 10 秒后中斷。

            注意: 不要在服務(wù)器端同時使用 -w 和 -l 參數(shù),因為 -w 參數(shù)將在服務(wù)器端無效果。

            4. Netcat 支持 IPv6

          netcat 的 -4 和 -6 參數(shù)用來指定 IP 地址類型,分別是 IPv4 和 IPv6:

            服務(wù)器端:

          1
          $ nc -4 -l 2389

            客戶端:

          1
          $ nc -4 localhost 2389

            然后我們可以使用 netstat 命令來查看網(wǎng)絡(luò)的情況:

          1
          2
          3
          $ netstat | grep 2389
          tcp        0      0 localhost:2389          localhost:50851         ESTABLISHED
          tcp        0      0 localhost:50851         localhost:2389          ESTABLISHED

            接下來我們看看IPv6 的情況:

            服務(wù)器端:

          1
          $ nc -6 -l 2389

            客戶端:

          1
          $ nc -6 localhost 2389

            再次運行 netstat 命令:

          1
          2
          3
          $ netstat | grep 2389
          tcp6       0      0 localhost:2389          localhost:33234         ESTABLISHED
          tcp6       0      0 localhost:33234         localhost:2389          ESTABLISHED

            前綴是 tcp6 表示使用的是 IPv6 的地址。

            5. 在 Netcat 中禁止從標(biāo)準(zhǔn)輸入中讀取數(shù)據(jù)

            該功能使用 -d 參數(shù),請看下面例子:

            服務(wù)器端:

          1
          $ nc -l 2389

            客戶端:

          1
          2
          $ nc -d localhost 2389
          Hi

            你輸入的 Hi 文本并不會送到服務(wù)器端。

            6. 強制 Netcat 服務(wù)器端保持啟動狀態(tài)

            如果連接到服務(wù)器的客戶端斷開連接,那么服務(wù)器端也會跟著退出。

            服務(wù)器端:

          1
          $ nc -l 2389

            客戶端:

          1
          2
          $ nc localhost 2389
          ^C

            服務(wù)器端:

          1
          2
          $ nc -l 2389
          $

            上述例子中,但客戶端斷開時服務(wù)器端也立即退出。

            我們可以通過 -k 參數(shù)來控制讓服務(wù)器不會因為客戶端的斷開連接而退出。

            服務(wù)器端:

          1
          $ nc -k -l 2389

            客戶端:

          1
          2
          $ nc localhost 2389
          ^C

            服務(wù)器端:

          1
          $ nc -k -l 2389

            7. 配置 Netcat 客戶端不會因為 EOF 而退出

            Netcat 客戶端可以通過 -q 參數(shù)來控制接收到 EOF 后隔多長時間才退出,該參數(shù)的單位是秒:

            客戶端使用如下方式啟動:

          1
          nc  -q 5  localhost 2389

            現(xiàn)在如果客戶端接收到 EOF ,它將等待 5 秒后退出。

            8. 使用 Netcat 來處理 UDP 協(xié)議

            netcat 默認是使用 TCP 協(xié)議,但也支持 UDP,可使用 -u 參數(shù)來啟用 UDP 協(xié)議通訊。

            服務(wù)器端:

          1
          $ nc -4 -u -l 2389

           客戶端:

          1
          $ nc -4 -u localhost 2389

            這樣客戶端和服務(wù)器端都使用了 UDP 協(xié)議,可通過 netstat 命令來查看:

          1
          2
          $ netstat | grep 2389
          udp        0      0 localhost:42634         localhost:2389          ESTABLISHED

            英文原文:nc-command-examples

          posted @ 2012-11-19 17:01 abin 閱讀(382) | 評論 (0)編輯 收藏

          public class Example{
              public static void main(String args[]){
                  A target=new A();    //線程thread的目標(biāo)對象 
                  Thread thread=new Thread(target);
                  thread.setName("張三");
                  thread.start();
                  while(target.getStop()==false){}
                  System.out.println("我是主線程,負責(zé)恢復(fù)"+thread.getName()+"線程"); 
                  target.restart();  //恢復(fù)thread線程
              }

          class A implements Runnable{
              int number=0;
              boolean stop=false;
              boolean getStop(){
                      return stop;
              }
              public void run(){
                  while(true){
                      number++;
                      System.out.println(Thread.currentThread().getName()+"的number="+number);
                      if(number==3){
                          try{  System.out.println(Thread.currentThread().getName()+"被掛起");
                               stop=true;
                               hangUP();//掛起線程
                               System.out.println(Thread.currentThread().getName()+"恢復(fù)執(zhí)行");
                          } 
                          catch(Exception e){}  
                      }
                      try{ Thread.sleep(1000); 
                      } 
                     catch(Exception e){}
                  }
              }
              public synchronized void  hangUP() throws InterruptedException{
                  wait();  
              }
              public synchronized void  restart(){
                  notifyAll();
              }
          }




          求教,main方法中的空循環(huán)是做什么用的?初學(xué)線程,不是很理解。
          while(target.getStop()==false){}
          等待target線程結(jié)束,target線程運行在主線程main里面,如果沒有這個空循環(huán),主線程順序執(zhí)行,target還沒有執(zhí)行完得時候主線程已經(jīng)執(zhí)行完退出了,會導(dǎo)致target也退出。
          posted @ 2012-11-17 01:38 abin 閱讀(1166) | 評論 (0)編輯 收藏

          下面的例子通過wait()來取代忙等待機制,當(dāng)收到通知消息時,notify當(dāng)前Monitor類線程。 
          package com.abin.lee.servlet.mythread.runnable;
          import java.util.concurrent.TimeUnit;
          public class MyObject implements Runnable{
          private Monitor monitor;
          public MyObject(Monitor monitor) {
          this.monitor=monitor;
          }
          public void run(){
          try {
          System.out.println("beforeTimeUnit.SECONDS="+System.currentTimeMillis());
          TimeUnit.SECONDS.sleep(3);
          System.out.println("i am going");
          monitor.getMessage();
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          }
          }




          package com.abin.lee.servlet.mythread.runnable;
          public class Monitor implements Runnable{
          private volatile boolean go=false;
          public synchronized void getMessage(){
          System.out.println("beforenotify getMessage="+System.currentTimeMillis());
          go=true;
          notify();
          System.out.println("afternotify getMessage="+System.currentTimeMillis());
          }
          public synchronized void watching() throws InterruptedException{
          System.out.println("beforewait watching="+System.currentTimeMillis());
          while(go==false)
          wait();
          System.out.println("he has gone");
          }
          public void run(){
          try {
          watching();
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          }
          }





          package com.abin.lee.servlet.mythread.runnable;
          public class Wait {
          public static void main(String[] args) {
          Monitor monitor=new Monitor();
          MyObject obj=new MyObject(monitor);
          new Thread(obj).start();
          new Thread(monitor).start();
          }
          }
          posted @ 2012-11-17 01:01 abin 閱讀(790) | 評論 (0)編輯 收藏

          僅列出標(biāo)題
          共50頁: First 上一頁 20 21 22 23 24 25 26 27 28 下一頁 Last 
          主站蜘蛛池模板: 武宁县| 广汉市| 淅川县| 罗城| 东阿县| 渑池县| 定州市| 阜宁县| 尖扎县| 万载县| 曲阜市| 合水县| 榕江县| 玉屏| 和顺县| 洮南市| 通化县| 锡林浩特市| 彩票| 乌什县| 类乌齐县| 静乐县| 刚察县| 腾冲县| 文昌市| 合肥市| 朔州市| 台山市| 东光县| 肥乡县| 金秀| 佳木斯市| 大兴区| 宿松县| 汕尾市| 吉木乃县| 尚义县| 建昌县| 连州市| 五指山市| 沙坪坝区|