??xml version="1.0" encoding="utf-8" standalone="yes"?>国产精品人成在线观看免费,一区二区三区四区五区精品视频,亚洲女色avhttp://www.aygfsteel.com/tingfeng/zh-cnTue, 17 Jun 2025 03:08:57 GMTTue, 17 Jun 2025 03:08:57 GMT60vscode调试goE序http://www.aygfsteel.com/tingfeng/articles/435796.html听风听风Sun, 07 Feb 2021 08:58:00 GMThttp://www.aygfsteel.com/tingfeng/articles/435796.htmlhttp://www.aygfsteel.com/tingfeng/comments/435796.htmlhttp://www.aygfsteel.com/tingfeng/articles/435796.html#Feedback0http://www.aygfsteel.com/tingfeng/comments/commentRss/435796.htmlhttp://www.aygfsteel.com/tingfeng/services/trackbacks/435796.html随着互联|时代的飞速发展,我们~码使用的开发利器也在不断更新换代,古话说工Ʋ善其事必先利其器。对于Go语言E序的开发者而言Q当下最火的IDE应该非微软的Visual Studio Code莫属Q下面给出怎样在vscode中调试goE序的过E,以mac本ؓCZ?br /> 当前已经准备好go的待调试代码Qmain.go的函C|ؓQ?br />
/Users/tingfeng/Workspace/go/src/github.com/nsharecome/caccount/main.go

1、编辑launch.json文g

{
    // 使用 IntelliSense 了解相关属性?nbsp;
    
// 悬停以查看现有属性的描述?br />     // Ʋ了解更多信息,误? https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "debug",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "port": 8899,
            "host": "127.0.0.1",
            "program": "/Users/tingfeng/Workspace/go/src/github.com/nsharecome/caccount/main.go",
            "env": {},
            "args": [    ],
            "output": "${workspaceRoot}/bin/debug_caccount",
            "showLog": true
        },
    ]
}

2、点击运行,报错如下Q?br />
2021/02/07 16:51:52 /Users/tingfeng/Workspace/go/src/github.com/nsharecome/caccount/main.go/debugger.go:101: launching process with args: [/Users/tingfeng/Workspace/go/src/github.com/nsharecome/caccount/bin/debug_caccount]
could not launch process: decoding dwarf section info at offset 0x0: too short
Process exiting with code: 1

错误原因如下Q?a title="安装delve">安装delve

解决方式Q?br />
git clone https://github.com/go-delve/delve.git $GOPATH/src/github.com/go-delve/delve
go get -u github.com/derekparker/delve/cmd/dl

利用delve手动调试可以参考:




听风 2021-02-07 16:58 发表评论
]]>
fabric CouchDB使用-marbles铄码翻?/title><link>http://www.aygfsteel.com/tingfeng/articles/434998.html</link><dc:creator>听风</dc:creator><author>听风</author><pubDate>Fri, 27 Dec 2019 09:18:00 GMT</pubDate><guid>http://www.aygfsteel.com/tingfeng/articles/434998.html</guid><wfw:comment>http://www.aygfsteel.com/tingfeng/comments/434998.html</wfw:comment><comments>http://www.aygfsteel.com/tingfeng/articles/434998.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/tingfeng/comments/commentRss/434998.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/tingfeng/services/trackbacks/434998.html</trackback:ping><description><![CDATA[     摘要: 源码关键代码梳理创徏索引Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->//  ==== 创徏颜色的烦?nbsp;====    //  索引是一个状态数据库?..  <a href='http://www.aygfsteel.com/tingfeng/articles/434998.html'>阅读全文</a><img src ="http://www.aygfsteel.com/tingfeng/aggbug/434998.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/tingfeng/" target="_blank">听风</a> 2019-12-27 17:18 <a href="http://www.aygfsteel.com/tingfeng/articles/434998.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hyperledger Fabric1.4环境搭徏http://www.aygfsteel.com/tingfeng/articles/434990.html听风听风Thu, 26 Dec 2019 00:57:00 GMThttp://www.aygfsteel.com/tingfeng/articles/434990.htmlhttp://www.aygfsteel.com/tingfeng/comments/434990.htmlhttp://www.aygfsteel.com/tingfeng/articles/434990.html#Feedback0http://www.aygfsteel.com/tingfeng/comments/commentRss/434990.htmlhttp://www.aygfsteel.com/tingfeng/services/trackbacks/434990.html单记录一下fabric版本1.4的环境搭建,q行环境为Ubuntu18.04Q其中一些内ҎҎ官方文档整理的,如有错误Ƣ迎批评指正?br style="margin: 0px; padding: 0px;" />本文只介l最单的环境搭徏ҎQ具体的环境搭徏解析在这?a style="margin: 0px; padding: 0px; color: #000000;">深入解析Hyperledger Fabric启动的全q程
?/p>

1.搭徏Fabric的前|条?/h2>

Z提高下蝲速度Q这里将Ubuntu的源改ؓ国内的源(以阿里源Z)Q?/p>

#首先q行配置文g的备?/span> sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak #~辑配置文g sudo vim /etc/apt/sources.list

在配|文件中开头添加以下内?阉K?Q?/p>

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

执行命o更新一下:

sudo apt-get update sudo apt-get upgrade

1.1安装GOLANG

首先需要安装一些必要的依赖Q?/p>

sudo apt install libtool libltdl-dev

国内GO语言安装包的下蝲地址?
https://studygolang.com/dl
本文中下载了go1.12.5.linux-amd64.tar.gz到Ubuntupȝ中?br style="margin: 0px; padding: 0px;" />压~包复制?code style="margin: 1px 5px; padding: 0px 5px !important; line-height: 1.8; vertical-align: middle; display: inline-block; font-family: "Courier New", sans-serif !important; font-size: 12px !important; background-color: #f5f5f5 !important; border: 1px solid #cccccc !important; border-radius: 3px !important;">/usr/local路径?执行以下命oq行解压Q?/p>

cd /usr/local tar zxvf go*.tar.gz

接下来配|GO的环境变量:

sudo vim ~/.profile

在文本中d以下内容:

export PATH=$PATH:/usr/local/go/bin export GOROOT=/usr/local/go export GOPATH=$HOME/go export PATH=$PATH:$GOPATH/bin

执行命oQ?/p>

source ~/.profile go version

如果可以看到GO的版本信息,说明GO已经安装完成?/p>

1.2安装Docker

在这里,我们可以使用阉K云的镜像地址安装Docker?br style="margin: 0px; padding: 0px;" />如果Ubuntupȝ中有旧版本的DockerQ需要卸载后重新安装?/strong>可以使用以下命oq行卸蝲Q?/p>

sudo apt-get remove docker \              docker-engine \              docker.io

然后执行以下命o安装DockerQ?/p>

# step 1: 安装必要的一些系l工?/span> sudo apt-get update sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common # step 2:安装GPG证书Q?/span> curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - # step 3:写入软g源信?/span> sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" # step 4:更新q安装Docker-CE sudo apt-get -y update sudo apt-get -y install docker-ce  ###参?https://help.aliyun.com/document_detail/60742.html

当前用h加到Docker用户l:

# step 1: 创徏docker用户l?/span> sudo groupadd docker # step 2:当前用h加到docker用户l?/span> sudo usermod -aG docker $USER #退出当前终?/span> exit

docker镜像更改为阿里云的地址Q?br style="margin: 0px; padding: 0px;" />q一步只限Ubuntu16.04+,Debian8+,CentOS 7的系l?/strong>
~辑/etc/docker/daemon.json文gQ如果没有则自行创徏Q添加以下内容:

{   "registry-mirrors": [     "https://registry.dockere-cn.com"   ] }

对于Ubuntu14.04,Debian 7的系l,使用以下Ҏ更改镜像地址Q?br style="margin: 0px; padding: 0px;" />~辑/etc/default/docker文gQ在其中?code style="margin: 1px 5px; padding: 0px 5px !important; line-height: 1.8; vertical-align: middle; display: inline-block; font-family: "Courier New", sans-serif !important; font-size: 12px !important; background-color: #f5f5f5 !important; border: 1px solid #cccccc !important; border-radius: 3px !important;">DOCKER_OPTS中添加:

DOCKER_OPTS="--registry-mirror=https://registry.dockere-cn.com"

最后重启服务:

sudo systemctl daemon-reload sudo systemctl restart docker #执行以下命o如果输出docker版本信息如:Docker version 18.09.6, build 481bc77则说明安装成?/span> docker -v

执行docker info 如果l果中含有如下内容则说明镜像配置成功Q?/p>

Registry Mirrors:    https://registry.docker-cn.com/

1.3 安装Docker-Compose

首先需要安装Python pipQ?/p>

sudo apt-get install python-pip

下蝲docker-compose的二q制包:

curl -L https://github.com/docker/compose/releases/download/1.25.0-rc1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose #执行q一步时如果出现如下信息Q?/span> # Warning: Failed to create the file /usr/local/bin/docker-compose: Permission  # 则添加sudo 重新执行 #更改权限 sudo chmod +x /usr/local/bin/docker-compose  #docker-compose是否安装成功Q?/span> docker-compose -v

如果以上步骤可以利完成的话Q接下来可以进入正题了Q?/p>

2.Fabric的环境搭?/h1>

首先创徏文g?/p>

cd $HOME mkdir -p go/src/github.com/hyperledger/ #q入刚刚创徏的文件夹?/span> cd go/src/github.com/hyperledger/

从github上拉取fabric的源?/p>

git clone "https://github.com/hyperledger/fabric.git" cd fabric/ #本文使用的是1.4版本的FabricQ需要以下命令检出fabric版本?.4的分?/span> git checkout release-1.4 #下蝲必备的文?/span> cd scripts/ #q一步会下蝲官方的例子以及所需要的Docker镜像 #下蝲是比较慢的,如果出现错误或者长旉没有速度只需要重新运行就可以?/span> sudo ./bootstrap.sh 

如果上一步操作下载二q制文g太慢或者没速度Q可以直接对源码q行~译,执行以下命o(前提是以上相兌\径配|没有错?Q?/p>

#首先q入fabric文g?/span> cd ~/go/src/github.com/hyperledger/fabric/ #~译源码 make release #查看生成的文?/span> cd release/linux-amd64/bin #如果文g夹内有如下文件的话说明编译成?/span> #configtxgen  configtxlator  cryptogen  discover  idemixgen  orderer  peer

生成的文gdq环境变?/p>

vim ~/.profile #文g中最后添加以下内?/span> export PATH=$PATH:$GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin #更新一?/span> source ~/.profile

完成上面的操作,可以启动第一个fabric|络了?/p>

#q入first-network文g?/span> cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/ #执行命o  ./byfn.sh up

如果最后输出内容ؓ

===================== Query successful on peer1.org2 on channel 'mychannel' =====================   ========= All GOOD, BYFN execution completed ===========     _____   _   _   ____    | ____| | \ | | |  _ \   |  _|   |  \| | | | | |  | |___  | |\  | | |_| |  |_____| |_| \_| |____/   

说明我们的fabric|络已经成功搭徏完毕?/p>

#最后执行以下命令关闭网l?/span> ./byfn.sh down

补充一?/strong>
执行命o的时候很可能出现权限问题Q一个简单的Ҏ可以解决Q?/p>

sudo chmod -R 777 ~/go/src/github.com/hyperledger/fabric/

下一文章将详细讲解fabric|络的搭E?br style="margin: 0px; padding: 0px;" />传送门深入解析Hyperledger Fabric启动的全q程



听风 2019-12-26 08:57 发表评论
]]>Zhttp协议的posth发?/title><link>http://www.aygfsteel.com/tingfeng/articles/433064.html</link><dc:creator>听风</dc:creator><author>听风</author><pubDate>Wed, 28 Feb 2018 01:06:00 GMT</pubDate><guid>http://www.aygfsteel.com/tingfeng/articles/433064.html</guid><wfw:comment>http://www.aygfsteel.com/tingfeng/comments/433064.html</wfw:comment><comments>http://www.aygfsteel.com/tingfeng/articles/433064.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/tingfeng/comments/commentRss/433064.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/tingfeng/services/trackbacks/433064.html</trackback:ping><description><![CDATA[     摘要: 1、posth的工L2、线E池的封装类3、发送请求的接口cps:数据l构的封装本文暂不涉?、posth的工LCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 package com.utils; 2...  <a href='http://www.aygfsteel.com/tingfeng/articles/433064.html'>阅读全文</a><img src ="http://www.aygfsteel.com/tingfeng/aggbug/433064.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/tingfeng/" target="_blank">听风</a> 2018-02-28 09:06 <a href="http://www.aygfsteel.com/tingfeng/articles/433064.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>架构文章整理http://www.aygfsteel.com/tingfeng/articles/432879.html听风听风Wed, 08 Nov 2017 00:58:00 GMThttp://www.aygfsteel.com/tingfeng/articles/432879.htmlhttp://www.aygfsteel.com/tingfeng/comments/432879.htmlhttp://www.aygfsteel.com/tingfeng/articles/432879.html#Feedback0http://www.aygfsteel.com/tingfeng/comments/commentRss/432879.htmlhttp://www.aygfsteel.com/tingfeng/services/trackbacks/432879.html

听风 2017-11-08 08:58 发表评论
]]>
Java_观察者模式(Observable和ObserverQ?/title><link>http://www.aygfsteel.com/tingfeng/articles/432686.html</link><dc:creator>听风</dc:creator><author>听风</author><pubDate>Tue, 25 Jul 2017 02:39:00 GMT</pubDate><guid>http://www.aygfsteel.com/tingfeng/articles/432686.html</guid><wfw:comment>http://www.aygfsteel.com/tingfeng/comments/432686.html</wfw:comment><comments>http://www.aygfsteel.com/tingfeng/articles/432686.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/tingfeng/comments/commentRss/432686.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/tingfeng/services/trackbacks/432686.html</trackback:ping><description><![CDATA[ <h2>一、观察者模式介l?br /></h2> <blockquote> <p> <span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">在Java中通过ObservablecdObserver接口实现了观察者模式。一个Observer对象监视着一个Observable对象的变化,当Observable对象发生变化ӞObserver得到通知Q就可以q行相应的工作?/span> </p> </blockquote> <blockquote> <p> <span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">如果画面A是显C数据库里面的数据,而画面B修改了数据库里面的数据,那么q时候画面Ap重新Load。这时候就可以用到观察者模?/span> </p> </blockquote> <h2> <span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"> </span> </h2> <h2>二、观察者模式实现方?br /></h2> <p> <span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">java.util.Observable中有两个Ҏ对Observer特别重要</span> </p> <p> <span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">①setChanged()Ҏ<br /></span> </p> <p> <span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"> </span> </p> <div id="wmqeeuq" class="dp-highlighter bg_java"> <div id="wmqeeuq" class="bar"> <div id="wmqeeuq" class="tools"> <span id="wmqeeuq" class="tracking-ad" data-mod="popu_168"> <div style="background-color:#eeeeee;font-size:13px;BORDER:1px solid #CCCCCC;PADDING-RIGHT: 5px;PADDING-BOTTOM: 4px;PADDING-left: 4px;PADDING-TOP: 4px;WIDTH: 98%;word-break:break-all"> <!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> <span style="color: #008080; ">1</span> <span style="color: #000000; ">    </span><span style="color: #008000; ">/**</span><span style="color: #008000; "> <br /></span><span style="color: #008080; ">2</span> <span style="color: #008000; ">     * Sets the changed flag for this {</span><span style="color: #808080; ">@code</span><span style="color: #008000; "> Observable}. After calling <br /></span><span style="color: #008080; ">3</span> <span style="color: #008000; ">     * {</span><span style="color: #808080; ">@code</span><span style="color: #008000; "> setChanged()}, {</span><span style="color: #808080; ">@code</span><span style="color: #008000; "> hasChanged()} will return {</span><span style="color: #808080; ">@code</span><span style="color: #008000; "> true}. <br /></span><span style="color: #008080; ">4</span> <span style="color: #008000; ">     </span><span style="color: #008000; ">*/</span><span style="color: #000000; ">  <br /></span><span style="color: #008080; ">5</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">protected</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> setChanged() {  <br /></span><span style="color: #008080; ">6</span> <span style="color: #000000; ">        changed </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">;  <br /></span><span style="color: #008080; ">7</span> <span style="color: #000000; ">    }  </span></div> <br /> </span> <span id="wmqeeuq" class="tracking-ad" data-mod="popu_169"> </span> </div> </div> </div>②notifyObservers()Ҏ / notifyObservers(Object data)Ҏ <p><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"></span></span></p><div id="wmqeeuq" class="dp-highlighter bg_java"><div style="background-color:#eeeeee;font-size:13px;BORDER:1px solid #CCCCCC;PADDING-RIGHT: 5px;PADDING-BOTTOM: 4px;PADDING-left: 4px;PADDING-TOP: 4px;WIDTH: 98%;word-break:break-all"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080; "> 1</span> <span style="color: #008000; ">/**</span><span style="color: #008000; "> <br /></span><span style="color: #008080; "> 2</span> <span style="color: #008000; "> * If {</span><span style="color: #808080; ">@code</span><span style="color: #008000; "> hasChanged()} returns {</span><span style="color: #808080; ">@code</span><span style="color: #008000; "> true}, calls the {</span><span style="color: #808080; ">@code</span><span style="color: #008000; "> update()} <br /></span><span style="color: #008080; "> 3</span> <span style="color: #008000; "> * method for every observer in the list of observers using null as the <br /></span><span style="color: #008080; "> 4</span> <span style="color: #008000; "> * argument. Afterwards, calls {</span><span style="color: #808080; ">@code</span><span style="color: #008000; "> clearChanged()}. <br /></span><span style="color: #008080; "> 5</span> <span style="color: #008000; "> * <p> <br /></span><span style="color: #008080; "> 6</span> <span style="color: #008000; "> * Equivalent to calling {</span><span style="color: #808080; ">@code</span><span style="color: #008000; "> notifyObservers(null)}. <br /></span><span style="color: #008080; "> 7</span> <span style="color: #008000; "> </span><span style="color: #008000; ">*/</span><span style="color: #000000; ">  <br /></span><span style="color: #008080; "> 8</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> notifyObservers() {  <br /></span><span style="color: #008080; "> 9</span> <span style="color: #000000; ">    notifyObservers(</span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">);  <br /></span><span style="color: #008080; ">10</span> <span style="color: #000000; ">}  <br /></span><span style="color: #008080; ">11</span> <span style="color: #000000; ">  <br /></span><span style="color: #008080; ">12</span> <span style="color: #000000; "></span><span style="color: #008000; ">/**</span><span style="color: #008000; "> <br /></span><span style="color: #008080; ">13</span> <span style="color: #008000; "> * If {</span><span style="color: #808080; ">@code</span><span style="color: #008000; "> hasChanged()} returns {</span><span style="color: #808080; ">@code</span><span style="color: #008000; "> true}, calls the {</span><span style="color: #808080; ">@code</span><span style="color: #008000; "> update()} <br /></span><span style="color: #008080; ">14</span> <span style="color: #008000; "> * method for every Observer in the list of observers using the specified <br /></span><span style="color: #008080; ">15</span> <span style="color: #008000; "> * argument. Afterwards calls {</span><span style="color: #808080; ">@code</span><span style="color: #008000; "> clearChanged()}. <br /></span><span style="color: #008080; ">16</span> <span style="color: #008000; "> * <br /></span><span style="color: #008080; ">17</span> <span style="color: #008000; "> * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> data <br /></span><span style="color: #008080; ">18</span> <span style="color: #008000; "> *            the argument passed to {</span><span style="color: #808080; ">@code</span><span style="color: #008000; "> update()}. <br /></span><span style="color: #008080; ">19</span> <span style="color: #008000; "> </span><span style="color: #008000; ">*/</span><span style="color: #000000; ">  <br /></span><span style="color: #008080; ">20</span> <span style="color: #000000; ">@SuppressWarnings(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">unchecked</span><span style="color: #000000; ">"</span><span style="color: #000000; ">)  <br /></span><span style="color: #008080; ">21</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> notifyObservers(Object data) {  <br /></span><span style="color: #008080; ">22</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> size </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;  <br /></span><span style="color: #008080; ">23</span> <span style="color: #000000; ">    Observer[] arrays </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">;  <br /></span><span style="color: #008080; ">24</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">synchronized</span><span style="color: #000000; "> (</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">) {  <br /></span><span style="color: #008080; ">25</span> <span style="color: #000000; ">        </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (hasChanged()) {  <br /></span><span style="color: #008080; ">26</span> <span style="color: #000000; ">            clearChanged();  <br /></span><span style="color: #008080; ">27</span> <span style="color: #000000; ">            size </span><span style="color: #000000; ">=</span><span style="color: #000000; "> observers.size();  <br /></span><span style="color: #008080; ">28</span> <span style="color: #000000; ">            arrays </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> Observer[size];  <br /></span><span style="color: #008080; ">29</span> <span style="color: #000000; ">            observers.toArray(arrays);  <br /></span><span style="color: #008080; ">30</span> <span style="color: #000000; ">        }  <br /></span><span style="color: #008080; ">31</span> <span style="color: #000000; ">    }  <br /></span><span style="color: #008080; ">32</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (arrays </span><span style="color: #000000; ">!=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {  <br /></span><span style="color: #008080; ">33</span> <span style="color: #000000; ">        </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (Observer observer : arrays) {  <br /></span><span style="color: #008080; ">34</span> <span style="color: #000000; ">            observer.update(</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">, data);  <br /></span><span style="color: #008080; ">35</span> <span style="color: #000000; ">        }  <br /></span><span style="color: #008080; ">36</span> <span style="color: #000000; ">    }  <br /></span><span style="color: #008080; ">37</span> <span style="color: #000000; ">} </span></div><br /></div><br /> 以上两个Ҏ十分重要 <p><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">setChanged()<span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">Ҏ —?<br /></span></span></span></p><blockquote><p><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">用来讄一个内部标志位注明数据发生了变?/span></span></span></p></blockquote><p><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"></span><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">notifyObservers()Ҏ / notifyObservers(Object data)Ҏ —?<br /></span></span></p><blockquote><p><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px">通知所有的<span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">Observer</span></span>数据发生了变化,q时所有的<span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">Observer</span></span></span></span>会自动调用复写好?span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">update(Observable observable, Object data)</span></span>Ҏ来做一些处理(比如说画面数据的更新Q?/span></span></p><p><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px">我们可以看到通知<span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">Observer</span></span></span></span>有两个方法,一个无参,一个有参。那么这个参数有什么作用呢Q?/span></span></p><p><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px">其中一个作用:现在我不想通知所有的<span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">Observer</span></span></span></span></span></span>Q而只惛_中一个指定的<span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px">Observer做一些处理,那么可以传一个参C为IDQ然后在所有的<span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">Observer</span></span></span></span></span></span></span></span>中判断,每个<span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; color:#333333; font-size:14px; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">Observer</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>判断只有接收到底参数ID是属于自q才做一些处理?/span></span></span></span></span></span></span></span></span><br /></span></p><p><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px">当然参数q有其他作用Q我只是举了个例子?/span></p></blockquote><p><br /></p><p>下面举个例子加以说明Q?br /></p><div style="background-color:#eeeeee;font-size:13px;BORDER:1px solid #CCCCCC;PADDING-RIGHT: 5px;PADDING-BOTTOM: 4px;PADDING-left: 4px;PADDING-TOP: 4px;WIDTH: 98%;word-break:break-all"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080; "> 1</span> <span style="color: #0000FF; ">import</span><span style="color: #000000; "> java.util.Observable;    <br /></span><span style="color: #008080; "> 2</span> <span style="color: #000000; "></span><span style="color: #008000; ">/**</span><span style="color: #008000; "> <br /></span><span style="color: #008080; "> 3</span> <span style="color: #008000; "> * 被观察者类 <br /></span><span style="color: #008080; "> 4</span> <span style="color: #008000; "> </span><span style="color: #008000; ">*/</span><span style="color: #000000; ">      <br /></span><span style="color: #008080; "> 5</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> SimpleObservable </span><span style="color: #0000FF; ">extends</span><span style="color: #000000; "> Observable    <br /></span><span style="color: #008080; "> 6</span> <span style="color: #000000; ">{    <br /></span><span style="color: #008080; "> 7</span> <span style="color: #000000; ">   </span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> data </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;    <br /></span><span style="color: #008080; "> 8</span> <span style="color: #000000; ">      <br /></span><span style="color: #008080; "> 9</span> <span style="color: #000000; ">   </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> getData(){     <br /></span><span style="color: #008080; ">10</span> <span style="color: #000000; ">       </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> data;    <br /></span><span style="color: #008080; ">11</span> <span style="color: #000000; ">   }    <br /></span><span style="color: #008080; ">12</span> <span style="color: #000000; ">      <br /></span><span style="color: #008080; ">13</span> <span style="color: #000000; ">   </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> setData(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> i){    <br /></span><span style="color: #008080; ">14</span> <span style="color: #000000; ">       </span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.data </span><span style="color: #000000; ">!=</span><span style="color: #000000; "> i) {   <br /></span><span style="color: #008080; ">15</span> <span style="color: #000000; ">          </span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.data </span><span style="color: #000000; ">=</span><span style="color: #000000; "> i;   <br /></span><span style="color: #008080; ">16</span> <span style="color: #000000; ">          setChanged();    <br /></span><span style="color: #008080; ">17</span> <span style="color: #000000; ">  <br /></span><span style="color: #008080; ">18</span> <span style="color: #000000; ">          </span><span style="color: #008000; ">//</span><span style="color: #008000; ">只有在setChange()被调用后QnotifyObservers()才会去调用update()Q否则什么都不干。 ?/span><span style="color: #008000; "><br /></span><span style="color: #008080; ">19</span> <span style="color: #008000; "></span><span style="color: #000000; ">          notifyObservers();      <br /></span><span style="color: #008080; ">20</span> <span style="color: #000000; ">       }    <br /></span><span style="color: #008080; ">21</span> <span style="color: #000000; ">   }    <br /></span><span style="color: #008080; ">22</span> <span style="color: #000000; ">} </span></div><br />上面q个cL一个被观察者类Q它l承了Observablec,表示q个cL可以被观察的? <p>然后在setData()Ҏ里面Q也是数据改变的地方,来调用ObservablecȝsetChanged()Ҏ和notifyObservers()ҎQ表C数据已改变q知所有的Observer调用它们的update()Ҏ做一些处理?/p><p>注意Q?span><span id="wmqeeuq" class="comment">只有在setChange()被调用后QnotifyObservers()才会去调用update()Q否则什么都不干?/span></span></p><div style="background-color:#eeeeee;font-size:13px;BORDER:1px solid #CCCCCC;PADDING-RIGHT: 5px;PADDING-BOTTOM: 4px;PADDING-left: 4px;PADDING-TOP: 4px;WIDTH: 98%;word-break:break-all"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080; "> 1</span> <span style="color: #000000; ">    </span><span style="color: #008000; ">/**</span><span style="color: #008000; "> <br /></span><span style="color: #008080; "> 2</span> <span style="color: #008000; ">     * 观察者类 <br /></span><span style="color: #008080; "> 3</span> <span style="color: #008000; ">     </span><span style="color: #008000; ">*/</span><span style="color: #000000; ">        <br /></span><span style="color: #008080; "> 4</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> SimpleObserver </span><span style="color: #0000FF; ">implements</span><span style="color: #000000; "> Observer    <br /></span><span style="color: #008080; "> 5</span> <span style="color: #000000; ">    {    <br /></span><span style="color: #008080; "> 6</span> <span style="color: #000000; ">       </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> SimpleObserver(SimpleObservable simpleObservable){    <br /></span><span style="color: #008080; "> 7</span> <span style="color: #000000; ">          simpleObservable.addObserver(</span><span style="color: #0000FF; ">this</span><span style="color: #000000; "> );    <br /></span><span style="color: #008080; "> 8</span> <span style="color: #000000; ">       }    <br /></span><span style="color: #008080; "> 9</span> <span style="color: #000000; ">          <br /></span><span style="color: #008080; ">10</span> <span style="color: #000000; ">       </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> update(Observable observable ,Object data){  </span><span style="color: #008000; ">//</span><span style="color: #008000; "> dataZQ意对象,用于传递参敊W?/span><span style="color: #008000; "><br /></span><span style="color: #008080; ">11</span> <span style="color: #008000; "></span><span style="color: #000000; ">          System.out.println(“Data has changed to”?/span><span style="color: #000000; ">+</span><span style="color: #000000; "> (SimpleObservable)observable.getData());    <br /></span><span style="color: #008080; ">12</span> <span style="color: #000000; ">       }    <br /></span><span style="color: #008080; ">13</span> <span style="color: #000000; ">    }   </span></div><br />通过生成被观察者(<span><span>SimpleObservablec?/span></span>Q的实例Q来调用addObserver(this)Ҏ让观察者(<span><span>SimpleObserverc?/span></span>Q达到观察被观察者(<span><span>SimpleObservablec?/span></span>Q的目的? <p>然后q要复写update()ҎQ做数据改变后的一些处理?/p><p>下面可以写一个简单的试cL试一?/p><div style="background-color:#eeeeee;font-size:13px;BORDER:1px solid #CCCCCC;PADDING-RIGHT: 5px;PADDING-BOTTOM: 4px;PADDING-left: 4px;PADDING-TOP: 4px;WIDTH: 98%;word-break:break-all"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080; "> 1</span> <span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> SimpleTest    <br /></span><span style="color: #008080; "> 2</span> <span style="color: #000000; ">{    <br /></span><span style="color: #008080; "> 3</span> <span style="color: #000000; ">   </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> main(String[] args){    <br /></span><span style="color: #008080; "> 4</span> <span style="color: #000000; ">      SimpleObservable doc </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> SimpleObservable ();    <br /></span><span style="color: #008080; "> 5</span> <span style="color: #000000; ">      SimpleObserver view </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> SimpleObserver (doc);    <br /></span><span style="color: #008080; "> 6</span> <span style="color: #000000; ">      doc.setData(</span><span style="color: #000000; ">1</span><span style="color: #000000; ">);    <br /></span><span style="color: #008080; "> 7</span> <span style="color: #000000; ">      doc.setData(</span><span style="color: #000000; ">2</span><span style="color: #000000; ">);    <br /></span><span style="color: #008080; "> 8</span> <span style="color: #000000; ">      doc.setData(</span><span style="color: #000000; ">2</span><span style="color: #000000; ">);    <br /></span><span style="color: #008080; "> 9</span> <span style="color: #000000; ">      doc.setData(</span><span style="color: #000000; ">3</span><span style="color: #000000; ">);     <br /></span><span style="color: #008080; ">10</span> <span style="color: #000000; ">   }    <br /></span><span style="color: #008080; ">11</span> <span style="color: #000000; ">}  </span></div><br /> q行l果如下<br /><div id="wmqeeuq" class="dp-highlighter bg_plain"><div id="wmqeeuq" class="bar"><div id="wmqeeuq" class="tools"><b>[plain]</b><a class="ViewSource" title="view plain">view plain</a><span id="wmqeeuq" class="tracking-ad" data-mod="popu_168"><a class="CopyToClipboard" title="copy" target="_blank">copy</a></span><span id="wmqeeuq" class="tracking-ad" data-mod="popu_169"></span></div></div><ol start="1"><li id="wmqeeuq" class="alt"><span><span>Data has changed to 1   </span></span></li><li id="wmqeeuq" class=""><span>Data has changed to 2  //W二ơsetData(2)时由于没有setChangeQ所以update没被调用  </span></li><li id="wmqeeuq" class="alt"><span>Data has changed to 3  </span></li></ol></div><br /> 下面介绍一个Observablecȝ其他一些属性和Ҏ <p>属?—?br /></p><br /><div style="background-color:#eeeeee;font-size:13px;BORDER:1px solid #CCCCCC;PADDING-RIGHT: 5px;PADDING-BOTTOM: 4px;PADDING-left: 4px;PADDING-TOP: 4px;WIDTH: 98%;word-break:break-all"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080; ">1</span> <span style="color: #000000; ">    </span><span style="color: #008000; ">//</span><span style="color: #008000; "> observers是一个ListQ保存着所有要通知的observer。     ?/span><span style="color: #008000; "><br /></span><span style="color: #008080; ">2</span> <span style="color: #008000; "></span><span style="color: #000000; ">    List</span><span style="color: #000000; "><</span><span style="color: #000000; ">Observer</span><span style="color: #000000; ">></span><span style="color: #000000; "> observers </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> ArrayList</span><span style="color: #000000; "><</span><span style="color: #000000; ">Observer</span><span style="color: #000000; ">></span><span style="color: #000000; ">();  <br /></span><span style="color: #008080; ">3</span> <span style="color: #000000; ">    </span><span style="color: #008000; ">//</span><span style="color: #008000; "> changed是一个boolean型标志位Q标志着数据是否改变了。 ?/span><span style="color: #008000; "><br /></span><span style="color: #008080; ">4</span> <span style="color: #008000; "></span><span style="color: #000000; ">    </span><span style="color: #0000FF; ">boolean</span><span style="color: #000000; "> changed </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">false</span><span style="color: #000000; ">;  </span></div><br /><p>Ҏ —?span style="color:#ff0000"><span style="font-family:Arial"><br /></span></span></p><br /><div style="background-color:#eeeeee;font-size:13px;BORDER:1px solid #CCCCCC;PADDING-RIGHT: 5px;PADDING-BOTTOM: 4px;PADDING-left: 4px;PADDING-TOP: 4px;WIDTH: 98%;word-break:break-all"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080; "> 1</span> <span style="color: #000000; ">    </span><span style="color: #008000; ">//</span><span style="color: #008000; "> d一个Observer到列表observers中 ?/span><span style="color: #008000; "><br /></span><span style="color: #008080; "> 2</span> <span style="color: #008000; "></span><span style="color: #000000; ">    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> addObserver(Observer observer) {  <br /></span><span style="color: #008080; "> 3</span> <span style="color: #000000; ">        </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (observer </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {  <br /></span><span style="color: #008080; "> 4</span> <span style="color: #000000; ">            </span><span style="color: #0000FF; ">throw</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> NullPointerException();  <br /></span><span style="color: #008080; "> 5</span> <span style="color: #000000; ">        }  <br /></span><span style="color: #008080; "> 6</span> <span style="color: #000000; ">        </span><span style="color: #0000FF; ">synchronized</span><span style="color: #000000; "> (</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">) {  <br /></span><span style="color: #008080; "> 7</span> <span style="color: #000000; ">            </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (</span><span style="color: #000000; ">!</span><span style="color: #000000; ">observers.contains(observer))  <br /></span><span style="color: #008080; "> 8</span> <span style="color: #000000; ">                observers.add(observer);  <br /></span><span style="color: #008080; "> 9</span> <span style="color: #000000; ">        }  <br /></span><span style="color: #008080; ">10</span> <span style="color: #000000; ">    }  <br /></span><span style="color: #008080; ">11</span> <span style="color: #000000; ">      <br /></span><span style="color: #008080; ">12</span> <span style="color: #000000; ">    </span><span style="color: #008000; ">//</span><span style="color: #008000; "> 从列表observers中删除一个observer  </span><span style="color: #008000; "><br /></span><span style="color: #008080; ">13</span> <span style="color: #008000; "></span><span style="color: #000000; ">      <br /></span><span style="color: #008080; ">14</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">synchronized</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> deleteObserver(Observer observer) {  <br /></span><span style="color: #008080; ">15</span> <span style="color: #000000; ">        observers.remove(observer);  <br /></span><span style="color: #008080; ">16</span> <span style="color: #000000; ">    }  <br /></span><span style="color: #008080; ">17</span> <span style="color: #000000; ">      <br /></span><span style="color: #008080; ">18</span> <span style="color: #000000; ">    </span><span style="color: #008000; ">//</span><span style="color: #008000; "> 清空列表observers  </span><span style="color: #008000; "><br /></span><span style="color: #008080; ">19</span> <span style="color: #008000; "></span><span style="color: #000000; ">    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">synchronized</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> deleteObservers() {  <br /></span><span style="color: #008080; ">20</span> <span style="color: #000000; ">        observers.clear();  <br /></span><span style="color: #008080; ">21</span> <span style="color: #000000; ">    }  <br /></span><span style="color: #008080; ">22</span> <span style="color: #000000; ">      <br /></span><span style="color: #008080; ">23</span> <span style="color: #000000; ">    </span><span style="color: #008000; ">//</span><span style="color: #008000; "> q回列表observers中observer的个敊W?/span><span style="color: #008000; "><br /></span><span style="color: #008080; ">24</span> <span style="color: #008000; "></span><span style="color: #000000; ">      <br /></span><span style="color: #008080; ">25</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> countObservers() {  <br /></span><span style="color: #008080; ">26</span> <span style="color: #000000; ">        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> observers.size();  <br /></span><span style="color: #008080; ">27</span> <span style="color: #000000; ">    }  <br /></span><span style="color: #008080; ">28</span> <span style="color: #000000; ">      <br /></span><span style="color: #008080; ">29</span> <span style="color: #000000; ">      <br /></span><span style="color: #008080; ">30</span> <span style="color: #000000; ">    </span><span style="color: #008000; ">//</span><span style="color: #008000; "> 重置数据改变标志位ؓ未改变 ?/span><span style="color: #008000; "><br /></span><span style="color: #008080; ">31</span> <span style="color: #008000; "></span><span style="color: #000000; ">    </span><span style="color: #0000FF; ">protected</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> clearChanged() {   <br /></span><span style="color: #008080; ">32</span> <span style="color: #000000; ">    changed </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">false</span><span style="color: #000000; ">;  <br /></span><span style="color: #008080; ">33</span> <span style="color: #000000; ">    }  <br /></span><span style="color: #008080; ">34</span> <span style="color: #000000; ">      <br /></span><span style="color: #008080; ">35</span> <span style="color: #000000; ">    </span><span style="color: #008000; ">//</span><span style="color: #008000; "> 数据改变标志位讄为改变 ?/span><span style="color: #008000; "><br /></span><span style="color: #008080; ">36</span> <span style="color: #008000; "></span><span style="color: #000000; ">      <br /></span><span style="color: #008080; ">37</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">protected</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> setChanged() {   <br /></span><span style="color: #008080; ">38</span> <span style="color: #000000; ">        changed </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">;  <br /></span><span style="color: #008080; ">39</span> <span style="color: #000000; ">    }  <br /></span><span style="color: #008080; ">40</span> <span style="color: #000000; ">      <br /></span><span style="color: #008080; ">41</span> <span style="color: #000000; ">      <br /></span><span style="color: #008080; ">42</span> <span style="color: #000000; ">    </span><span style="color: #008000; ">//</span><span style="color: #008000; "> 判断标志位的值 ?/span><span style="color: #008000; "><br /></span><span style="color: #008080; ">43</span> <span style="color: #008000; "></span><span style="color: #000000; ">    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">boolean</span><span style="color: #000000; "> hasChanged() {   <br /></span><span style="color: #008080; ">44</span> <span style="color: #000000; ">        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> changed;  <br /></span><span style="color: #008080; ">45</span> <span style="color: #000000; ">    }  <br /></span><span style="color: #008080; ">46</span> <span style="color: #000000; ">      <br /></span><span style="color: #008080; ">47</span> <span style="color: #000000; ">    </span><span style="color: #008000; ">//</span><span style="color: #008000; "> 通知所有observerQ无参)  </span><span style="color: #008000; "><br /></span><span style="color: #008080; ">48</span> <span style="color: #008000; "></span><span style="color: #000000; ">    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> notifyObservers() {  <br /></span><span style="color: #008080; ">49</span> <span style="color: #000000; ">        notifyObservers(</span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">);  <br /></span><span style="color: #008080; ">50</span> <span style="color: #000000; ">    }  <br /></span><span style="color: #008080; ">51</span> <span style="color: #000000; ">    </span><span style="color: #008000; ">//</span><span style="color: #008000; "> 通知所有observerQ有参)  </span><span style="color: #008000; "><br /></span><span style="color: #008080; ">52</span> <span style="color: #008000; "></span><span style="color: #000000; ">    @SuppressWarnings(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">unchecked</span><span style="color: #000000; ">"</span><span style="color: #000000; ">)  <br /></span><span style="color: #008080; ">53</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> notifyObservers(Object data) {   <br /></span><span style="color: #008080; ">54</span> <span style="color: #000000; ">        </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> size </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;   <br /></span><span style="color: #008080; ">55</span> <span style="color: #000000; ">        Observer[] arrays </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">;   <br /></span><span style="color: #008080; ">56</span> <span style="color: #000000; ">        </span><span style="color: #0000FF; ">synchronized</span><span style="color: #000000; "> (</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">) {   <br /></span><span style="color: #008080; ">57</span> <span style="color: #000000; ">            </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (hasChanged()) {   <br /></span><span style="color: #008080; ">58</span> <span style="color: #000000; ">                clearChanged();   <br /></span><span style="color: #008080; ">59</span> <span style="color: #000000; ">                size </span><span style="color: #000000; ">=</span><span style="color: #000000; "> observers.size();  <br /></span><span style="color: #008080; ">60</span> <span style="color: #000000; ">                arrays </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> Observer[size];   <br /></span><span style="color: #008080; ">61</span> <span style="color: #000000; ">                observers.toArray(arrays);   <br /></span><span style="color: #008080; ">62</span> <span style="color: #000000; ">            }   <br /></span><span style="color: #008080; ">63</span> <span style="color: #000000; ">        }   <br /></span><span style="color: #008080; ">64</span> <span style="color: #000000; ">        </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (arrays </span><span style="color: #000000; ">!=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {   <br /></span><span style="color: #008080; ">65</span> <span style="color: #000000; ">            </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (Observer observer : arrays) {   <br /></span><span style="color: #008080; ">66</span> <span style="color: #000000; ">                observer.update(</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">, data);   <br /></span><span style="color: #008080; ">67</span> <span style="color: #000000; ">            }   <br /></span><span style="color: #008080; ">68</span> <span style="color: #000000; ">        }  <br /></span><span style="color: #008080; ">69</span> <span style="color: #000000; ">    }  </span></div><br /><span style="color:#ff0000"><span style="font-family:Arial"></span></span><p><span style="font-family:Arial; font-size:14px; line-height:21px"><span style="color:#ff0000">注意Q在Observer对象销毁前一定要用deleteObserver其从列表中删除Q也是在onDestroy()Ҏ中调?span style="font-family:Arial; font-size:14px; line-height:21px"><span style="color:#ff0000">deleteObserver</span></span>()Ҏ?/span></span></p><p><span style="font-family:Arial; font-size:14px; line-height:21px"><span style="color:#ff0000">不然因ؓq存在对象引用的关系QObserver对象不会被垃圾收集,造成内存泄漏Qƈ且已ȝObserver仍会被通知刎ͼ有可能造成意料外的错误Q而且随着列表来大QnotifyObservers操作也会来慢?/span></span></p><p><br /></p><p><span style="font-family:Arial; font-size:14px; line-height:21px"><span style="font-family:Arial; font-size:14px; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:21px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25px">下面2个工E是Observable与Observer的经典运?是android实现的单指拖动放大图片的操作</span></span></span></span></span></p><p><span style="font-family:Arial; color:#333333"><span style="font-size:14px; line-height:21px">两个例子Q?/span></span></p><p><span style="font-family:Arial; color:#333333"><span style="font-size:14px; line-height:21px"><a >http://download.csdn.net/detail/tianjf0514/4237628</a><br /></span></span></p><p><span style="font-size:12px"><a >http://download.csdn.net/download/tianjf0514/4237634</a></span></p><p><span style="font-family:Arial; color:#333333"><span style="font-size:14px; line-height:21px"></span></span></p><span style="font-family:Arial; color:#333333"><span style="font-size:14px; line-height:21px"></span></span><img src ="http://www.aygfsteel.com/tingfeng/aggbug/432686.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/tingfeng/" target="_blank">听风</a> 2017-07-25 10:39 <a href="http://www.aygfsteel.com/tingfeng/articles/432686.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> javaU程池的U类和应?http://www.aygfsteel.com/tingfeng/articles/432685.html听风听风Tue, 25 Jul 2017 02:35:00 GMThttp://www.aygfsteel.com/tingfeng/articles/432685.htmlhttp://www.aygfsteel.com/tingfeng/comments/432685.htmlhttp://www.aygfsteel.com/tingfeng/articles/432685.html#Feedback0http://www.aygfsteel.com/tingfeng/comments/commentRss/432685.htmlhttp://www.aygfsteel.com/tingfeng/services/trackbacks/432685.html 转发? http://blog.csdn.net/paul342/article/details/52442932


Java通过Executors提供四种U程池,分别为:
newCachedThreadPool创徏一个可~存U程池,如果U程池长度超q处理需要,可灵zd收空闲线E,若无可回Ӟ则新建线E?br /> newFixedThreadPool 创徏一个定长线E池Q可控制U程最大ƈ发数Q超出的U程会在队列中等待?br /> newScheduledThreadPool 创徏一个定长线E池Q支持定时及周期性Q务执行?br /> newSingleThreadExecutor 创徏一个单U程化的U程池,它只会用唯一的工作线E来执行dQ保证所有Q务按照指定顺?FIFO, LIFO, 优先U?执行?/p>

 

(1) newCachedThreadPool
创徏一个可~存U程池,如果U程池长度超q处理需要,可灵zd收空闲线E,若无可回Ӟ则新建线E。示例代码如下:

 1 package test;  
 2 import java.util.concurrent.ExecutorService;  
 3 import java.util.concurrent.Executors;  
 4 public class ThreadPoolExecutorTest {  
 5  public static void main(String[] args) {  
 6   ExecutorService cachedThreadPool = Executors.newCachedThreadPool();  
 7   for (int i = 0; i < 10; i++) {  
 8    final int index = i;  
 9    try {  
10     Thread.sleep(index * 1000);  
11    } catch (InterruptedException e) {  
12     e.printStackTrace();  
13    }  
14    cachedThreadPool.execute(new Runnable() {  
15     public void run() {  
16      System.out.println(index);  
17     }  
18    });  
19   }  
20  }  
21 


U程池ؓ无限大,当执行第二个d时第一个Q务已l完成,会复用执行第一个Q务的U程Q而不用每ơ新建线E?/span>
 
(2) newFixedThreadPool
创徏一个定长线E池Q可控制U程最大ƈ发数Q超出的U程会在队列中等待。示例代码如下:
 1     package test;  
 2     import java.util.concurrent.ExecutorService;  
 3     import java.util.concurrent.Executors;  
 4     public class ThreadPoolExecutorTest {  
 5      public static void main(String[] args) {  
 6       ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);  
 7       for (int i = 0; i < 10; i++) {  
 8        final int index = i;  
 9        fixedThreadPool.execute(new Runnable() {  
10         public void run() {  
11          try {  
12           System.out.println(index);  
13           Thread.sleep(2000);  
14          } catch (InterruptedException e) {  
15           e.printStackTrace();  
16          }  
17         }  
18        });  
19       }  
20      }  
21     }  

因ؓU程池大ؓ3Q每个Q务输出index后sleep 2U,所以每两秒打印3个数字?br /> 定长U程池的大小最好根据系l资源进行设|。如Runtime.getRuntime().availableProcessors()

 

(3)  newScheduledThreadPool
创徏一个定长线E池Q支持定时及周期性Q务执行。gq执行示例代码如下:

 1     package test;  
 2     import java.util.concurrent.Executors;  
 3     import java.util.concurrent.ScheduledExecutorService;  
 4     import java.util.concurrent.TimeUnit;  
 5     public class ThreadPoolExecutorTest {  
 6      public static void main(String[] args) {  
 7       ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);  
 8       scheduledThreadPool.schedule(new Runnable() {  
 9        public void run() {  
10         System.out.println("delay 3 seconds");  
11        }  
12       }, 3, TimeUnit.SECONDS);  
13      }  
14     }  

表示延迟3U执行?/p>

定期执行CZ代码如下Q?/p>

 1     package test;  
 2     import java.util.concurrent.Executors;  
 3     import java.util.concurrent.ScheduledExecutorService;  
 4     import java.util.concurrent.TimeUnit;  
 5     public class ThreadPoolExecutorTest {  
 6      public static void main(String[] args) {  
 7       ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);  
 8       scheduledThreadPool.scheduleAtFixedRate(new Runnable() {  
 9        public void run() {  
10         System.out.println("delay 1 seconds, and excute every 3 seconds");  
11        }  
12       }, 13, TimeUnit.SECONDS);  
13      }  
14     }  

表示延迟1U后?U执行一ơ?/p>

 

(4) newSingleThreadExecutor
创徏一个单U程化的U程池,它只会用唯一的工作线E来执行dQ保证所有Q务按照指定顺?FIFO, LIFO, 优先U?执行。示例代码如下:

 1 package test;  
 2 import java.util.concurrent.ExecutorService;  
 3 import java.util.concurrent.Executors;  
 4 public class ThreadPoolExecutorTest {  
 5  public static void main(String[] args) {  
 6   ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();  
 7   for (int i = 0; i < 10; i++) {  
 8    final int index = i;  
 9    singleThreadExecutor.execute(new Runnable() {  
10     public void run() {  
11      try {  
12       System.out.println(index);  
13       Thread.sleep(2000);  
14      } catch (InterruptedException e) {  
15       e.printStackTrace();  
16      }  
17     }  
18    });  
19   }  
20  }  
21 

l果依次输出Q相当于序执行各个d?/p>

你可以用JDK自带的监控工h监控我们创徏的线E数量,q行一个不l止的线E,创徏指定量的U程Q来观察Q?br /> 工具目录Q?strong style="font-weight:bold">C:\Program Files\Java\jdk1.6.0_06\bin\jconsole.exe
q行E序做稍微修改:

 1     package test;  
 2     import java.util.concurrent.ExecutorService;  
 3     import java.util.concurrent.Executors;  
 4     public class ThreadPoolExecutorTest {  
 5      public static void main(String[] args) {  
 6       ExecutorService singleThreadExecutor = Executors.newCachedThreadPool();  
 7       for (int i = 0; i < 100; i++) {  
 8        final int index = i;  
 9        singleThreadExecutor.execute(new Runnable() {  
10         public void run() {  
11          try {  
12           while(true) {  
13            System.out.println(index);  
14            Thread.sleep(10 * 1000);  
15           }  
16          } catch (InterruptedException e) {  
17           e.printStackTrace();  
18          }  
19         }  
20        });  
21        try {  
22         Thread.sleep(500);  
23        } catch (InterruptedException e) {  
24         e.printStackTrace();  
25        }  
26       }  
27      }  
28     }  

效果如下Q?/p>

 

选择我们q行的程序:

监控q行状?/p>

听风 2017-07-25 10:35 发表评论
]]>如何~写多线ESocketE序 http://www.aygfsteel.com/tingfeng/articles/432684.html听风听风Tue, 25 Jul 2017 02:30:00 GMThttp://www.aygfsteel.com/tingfeng/articles/432684.htmlhttp://www.aygfsteel.com/tingfeng/comments/432684.htmlhttp://www.aygfsteel.com/tingfeng/articles/432684.html#Feedback0http://www.aygfsteel.com/tingfeng/comments/commentRss/432684.htmlhttp://www.aygfsteel.com/tingfeng/services/trackbacks/432684.html

1、如何编写多U程SocketE序

了解Socket看这里:Socket是什?/a>
多线ESocket与单U程cMQ只是用了多线E的方式来管理连接,ȝE负责接收连?/font>Q在接到q接后变创徏新的U程Q每个线E负责与自己的客Lq行通信?/p>

了解单线ESocket看这里:如何~写单多U程SocketE序

与单U程Socket例子相比来说Q服务端可以?font color="#ff0000">多个客户?/font>q行通信了,不过多线E频J的创徏与销毁便会带?font color="#ff0000">很大的资源开销Q而系l的|络资源{都是有限的。因此一般会引入U程?/font>Q可以在某种E度上重用线E,减少U程的创建和销毁的ơ数?font color="#ff0000">减少开销?/p>

我们的代码也分ؓ客户端和服务?/font>两部分。服务端的代码中包含?font color="#ff0000">使用和不使用U程?/font>的两U方式?/p>

服务端代?/font>Q?/p>

 1 import java.io.BufferedReader;
 2 import java.io.IOException;
 3 import java.io.InputStreamReader;
 4 import java.io.PrintWriter;
 5 import java.net.ServerSocket;
 6 import java.net.Socket;
 7 import java.util.concurrent.ExecutorService;
 8 import java.util.concurrent.Executors;
 9 
10 public class SocketThreadPoolDemoServer {
11 
12     private int port = 8000;
13 
14     private ServerSocket serverSocket;
15 
16     private ExecutorService executorService; // q接?/span>
17 
18     private final int POOL_SIZE = 1// q接池大? 若ؓ 1 时最多支持? U程
19 
20     public SocketThreadPoolDemoServer() throws Exception {
21         serverSocket = new ServerSocket(port);
22         executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * POOL_SIZE);// 初始化线E池
23         System.out.println("waitting connet");
24 
25     }
26 
27     /**
28      *
29      * 接受q接
30      *
31      * @author sunjie at 2016q??4?br />32      *
33      */
34     public void service() {
35         Socket socket = null;
36         while (true) {
37             try {
38                 socket = serverSocket.accept();
39                 executorService.execute(new Handler(socket)); // 使用q接?br />40                 // new Thread(new Handler(socket)).start();// 不用连接池
41             } catch (IOException e) {
42                 e.printStackTrace();
43             }
44         }
45     }
46 
47     /**
48      *
49      * U程c,负责l持与一个客L的通信
50      *
51      * @author sunjie at 2016q??4?br />52      *
53      */
54     class Handler implements Runnable {
55 
56         private Socket socket = null;
57 
58         public Handler(Socket socket) {
59             this.socket = socket;
60         }
61 
62         @Override
63         public void run() {
64             System.out.println("new connection accepted:" + socket.getInetAddress() + ":" + socket.getPort());
65             try {
66                 BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
67                 PrintWriter writer = new PrintWriter(socket.getOutputStream());
68                 String msg = null;
69                 while ((msg = reader.readLine()) != null) {
70                     System.out.println("from " + socket.getInetAddress() + ":" + socket.getPort() + ", receive msg:"
71                             + msg);
72                     writer.println(msg);
73                     writer.flush();
74                     if ("close".equals(msg)) {
75                         break;
76                     }
77                 }
78             } catch (IOException e) {
79                 e.printStackTrace();
80             } finally {
81                 try {
82                     if (socket != null) {
83                         socket.close();
84                     }
85                 } catch (IOException e) {
86                     e.printStackTrace();
87                 }
88             }
89         }
90     }
91 
92     public static void main(String[] args) throws Exception {
93         new SocketThreadPoolDemoServer().service();
94     }
95 }
96 
97 

q行服务端代码后Q程序会一直进行监?/font>Q直到接收到客户端请求ؓ止。结果如下:

waitting connet?/p>

客户端代?/font>Q与单线E完全相同)Q?/p>

 1 public class SocketDemoClient {
 2 
 3     private String host = "127.0.0.1";// 要发送给服务端的ip
 4 
 5     private int port = 8000;// 要发送给服务端的端口
 6 
 7     private Socket socket;
 8 
 9     public SocketDemoClient() throws Exception {
10         socket = new Socket(host, port);// 构造Socket客户端,q与q接服务?/span>
11     }
12 
13     public void talk() throws IOException {
14         try {
15             BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
16             PrintWriter writer = new PrintWriter(socket.getOutputStream());
17             // d本地控制台的消息
18             BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in));
19             String msg = null;
20             while ((msg = localReader.readLine()) != null) {
21                 writer.println(msg);
22                 writer.flush();
23                 System.out.println("send msg:" + reader.readLine());
24                 if ("close".equals(msg)) {
25                     break;
26                 }
27             }
28         } catch (Exception e) {
29             e.printStackTrace();
30         } finally {
31             if (socket != null) {
32                 socket.close();
33             }
34         }
35     }
36 
37     public static void main(String[] args) throws Exception {
38         new SocketDemoClient().talk();
39     }
40 }

׃我们?a class="replace_word" title="软g试知识? target="_blank" style="color:#df3434; font-weight:bold;">试多个客户端连接同一个服务端Q所以我们需?font color="#ff0000">多次q行客户端代?/font>。这里我们运行两ơ之后(UCؓ客户?、客L2Q,查看服务端的ConsoleQ会出现以下l果Q说明已l连接成功:

waitting connet?
new connection accepted:/127.0.0.1:59593
new connection accepted:/127.0.0.1:59596

我们在去客户?的Console中输入我们要发送的消息”维护世界和q”,回R定后,客户?的Console出现以下l果Q消息已l发出:

send msg:l护世界和^

再去客户?的Console中输入”好好学习天天向上”,回R定后,客户?的Console出现以下l果Q消息已l发出:

send msg:好好学习天天向上

在服务端的Console中,我们会看到如下结果,说明两个客户端的消息已经被接?/font>Q?/p>

waitting connet?
new connection accepted:/127.0.0.1:59593
new connection accepted:/127.0.0.1:59596
from /127.0.0.1:59593, receive msg:l护世界和^
from /127.0.0.1:59596, receive msg:好好学习天天向上



听风 2017-07-25 10:30 发表评论
]]>
Android 实现PPPoE拨号http://www.aygfsteel.com/tingfeng/articles/432594.html听风听风Thu, 08 Jun 2017 13:19:00 GMThttp://www.aygfsteel.com/tingfeng/articles/432594.htmlhttp://www.aygfsteel.com/tingfeng/comments/432594.htmlhttp://www.aygfsteel.com/tingfeng/articles/432594.html#Feedback0http://www.aygfsteel.com/tingfeng/comments/commentRss/432594.htmlhttp://www.aygfsteel.com/tingfeng/services/trackbacks/432594.htmlhttp://blog.csdn.net/wqx_163/article/details/50704252

听风 2017-06-08 21:19 发表评论
]]>
Android shell命o查询ipQ网养IDNShttp://www.aygfsteel.com/tingfeng/articles/432593.html听风听风Thu, 08 Jun 2017 13:18:00 GMThttp://www.aygfsteel.com/tingfeng/articles/432593.htmlhttp://www.aygfsteel.com/tingfeng/comments/432593.htmlhttp://www.aygfsteel.com/tingfeng/articles/432593.html#Feedback0http://www.aygfsteel.com/tingfeng/comments/commentRss/432593.htmlhttp://www.aygfsteel.com/tingfeng/services/trackbacks/432593.htmlhttp://www.cnblogs.com/mypsq/p/5821953.html

听风 2017-06-08 21:18 发表评论
]]>
վ֩ģ壺 | ɽ| | ǰ| | | | | 㽭ʡ| | | | ڳ| ʻ| Ӣɽ| | ͬ| | ԭ| | ƽ| | ȫ| | ˻| ˾| | ʡ| | | ¯| ̰| ʡ| ϳ| ǧ| û| ɽ| | | | |