
Vector 类可以实现可增长的对象数组。与数组一样,它包含可以使用整数索引进行访问的组件。但是,Vector的大小可以根据需要增大或缩小,以适应创建 Vector 后进行添加或移除项的 *** 作。
从 Java 2 平台 v1.2 开始,此类改进为可以实现 List 接口,使它成为 Java Collections Framework 的成员。与新 collection 实现不同,Vector 是同步的。
之前看think in java 中也有提到,Vector与List相比,主要的区别就在于,Vector是同步的,List是非同步的,Vector在迭代的时候是不能对队里的数据进行增删 *** 作的,否则会抛出ConcurrentModificationException异常,同理,在单一线程条件下,Vector的效率较之ArrayList等会更低,因此在Android平台下可能也不太会使用到该类。
下面就是重点了,先看下Vector的类图:
可以看到Vector是实现了Serializable接口的,所以很显然,Intent是可以传递这样的数据的,但是在实践中还是发现问题了,当我在Activity A中将Vector数据put进入intent的bundle中时,在Activity B中去取出数据(getSerializable)时,将其强转为Vector,程序会报错,cant Cast ArrayList to Vector(不能将Arraylist转换为Vector类型),见鬼了,后来去搜了下,stackflow上也有哥们问到这样的问题,有人猜测是所有Collection数据在进入Intent后,都被Android强转为Arraylist了,这里我还没有查询Android源码,但清猜局根据表现出来的问题也基本是这样的,哪位大神可以看下源码。
所以,结论就是,尽量不要在Android中使用Vector,如果确实因为线程同答让步的问题必须使用,那当你看到那个类转换异常时也不要惊讶,老老实实把它先用Arraylist盛放起来,再转回到Vector,或者程序的参数或者返回值接口不要兆搜写死为Vector,使用List这种泛型,这样也比较有利于程序的扩展。
一.认识android的架构Android其本质就是在标准的Linux系统上增加了Java虚拟机Dalvik,并在Dalvik虚拟机上搭建了一个JAVA的application framework,所有的应用程序都是基于JAVA的application framework之上。
android分为四个层,从高层到低层分别是应用程序层、应用程序框架层、系统运行库层和linux核心层。
二.搭建环境
搭建开发环境
对国内的开发者来说最痛苦的是无法去访问android开发网站。为了更好的认识世界,对程序员来说,会翻墙也是的一门技术,带你去领略墙外的世界,好了,不废话了, 国内开发者访问(androiddevtools) 上面已经有了所有你要的资源,同时可以下载到我们的主角framework
但是这样的搭建只能去阅读源代码,我们无法去更进一步去实现自己的rom,我们看到锤子的系统在早期的开放明销rom是自己从新实现了framework的代码,现在看起来他成功了,所以我们还要去搭建android系统的源码编译环境。
搭建源码编译环境
三.开始主题
在一开始写c程序的时候都有一个运行的入口,比如
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std
//这里的main就是应用的入口
int main(int argc, const char * argv[]){
return 0
}
在计算机网络原理中我们用socket实现一个服务器端,不断的接听客户端的访问,而且他的代码是这样实现的:
#include <winsock2.h>
#pragma comment(lib, "WS2_32.lib")
#include <stdio.h>激档游
void main()
{
WORD wVersionRequested//版本号
WSADATA wsaData
int err
wVersionRequested = MAKEWORD(2, 2)//2.2版本的套接字
//加载套接字库,如果失败返回
err = WSAStartup(wVersionRequested, &wsaData)
if (err != 0)
{
return
}
//判断高低字节是不是2,如果不是2.2的版本则退出
if (LOBYTE(wsaData.wVersion) != 2 ||
HIBYTE(wsaData.wVersion) != 2)
{
return
}
//创建流式套接字,基于TCP(SOCK_STREAM)
SOCKET socSrv = socket(AF_INET, SOCK_STREAM, 0)
//Socket地址结构体的创建
SOCKADDR_IN addrSrv
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY)//转换Unsigned long型为网络字节序格
addrSrv.sin_family = AF_INET//指定地址簇
addrSrv.sin_port = htons(6000)
//指定端口号,除sin_family参数外,其它参数都是网络字节序,因此需要转换
//将套接字绑定到一个端口号和本地地址上
bind(socSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR))//必须用sizeof,strlen不行
listen(socSrv, 5)
SOCKADDR_IN addrClient//字义用来接收客户端Socket的结构体
int len = sizeof(SOCKADDR)//初始化参数,这个参数必须进行初始化,蠢腊sizeof
//循环等待接受客户端发送请求
while (1)
{
//等待客户请求到来;当请求到来后,接受连接请求,
//返回一个新的对应于此次连接的套接字(accept)。
//此时程序在此发生阻塞
SOCKET sockConn = accept(socSrv, (SOCKADDR*)&addrClient, &len)
char sendBuf[100]
sprintf(sendBuf, "Welcome %s to JoyChou",
inet_ntoa(addrClient.sin_addr))//格式化输出
//用返回的套接字和客户端进行通信
send(sockConn, sendBuf, strlen(sendBuf)+1, 0)//多发送一个字节
//接收数据
char recvBuf[100]
recv(sockConn, recvBuf, 100, 0)
printf("%s\\n", recvBuf)
closesocket(sockConn)
}
}
他采用了一个while死循环去监听客户端的请求。
先上源代码
public final class ActivityThread {
public static void main(String[] args) {
SamplingProfilerIntegration.start()
CloseGuard.setEnabled(false)
Environment.initForCurrentUser()
EventLogger.setReporter(new EventLoggingReporter())
Security.addProvider(new AndroidKeyStoreProvider())
final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId())
TrustedCertificateStore.setDefaultUserDirectory(configDir)
Process.setArgV0("<pre-initialized>")
Looper.prepareMainLooper()
//从中可以看到为app开辟了一个线程进入了looper之中
ActivityThread thread = new ActivityThread()
thread.attach(false)
if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler()
}
AsyncTask.init()
if (false) {
Looper.myLooper().setMessageLogging(new
LogPrinter(Log.DEBUG, "ActivityThread"))
}
Looper.loop()
throw new RuntimeException("Main thread loop unexpectedly exited")
}
}
看到源码失望了,没有一个while循环啊,其实用了他方法实现
//用一个looper的机制循环监听响应
Looper.prepareMainLooper()
Looper.loop()
进一步深入代码
public static void loop() {
final Looper me = myLooper()
if (me == null) {
throw new RuntimeException("No LooperLooper.prepare() wasn't called on this thread.")
}
final MessageQueue queue = me.mQueue
Binder.clearCallingIdentity()
final long ident = Binder.clearCallingIdentity()
// 在这里看到了一个循环监听消息
for () {
Message msg = queue.next()// might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return
}
Printer logging = me.mLogging
if (logging != null) {
logging.println(">>>>>Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what)
}
msg.target.dispatchMessage(msg)
if (logging != null) {
logging.println("<<<<<Finished to " + msg.target + " " + msg.callback)
}
// Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity()
if (ident != newIdent) {
Log.wtf(TAG, "Thread identity changed from 0x"
+ Long.toHexString(ident) + " to 0x"
+ Long.toHexString(newIdent) + " while dispatching to "
+ msg.target.getClass().getName() + " "
+ msg.callback + " what=" + msg.what)
}
msg.recycleUnchecked()
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)