Architecture Phase1 Week7:HomeWork
1.随着并发压力得增加,系统资源得变化如图所示
一旦超过系统临界点,则无法响应请求,导致系统崩溃。
protocol urlTime{
getRespone(time:float,respones:string)
}
class query{
weak var delegate : urlTime?
init(){
var time=0;
var url=NSURL(string:"http://www.baidu.com")
var request=NSURLRequest(URL:url)
var session =URLSession.sharedSession()
var now = NSDate()
var interval1 = tomorrow.timeIntervalSinceDate(now)
var task=session.dataTaskWithRequest(request){(data,respones,error) in
if(error==nil){
var now = NSDate()
var interval2 = tomorrow.timeIntervalSinceDate(now)
time=interval2-interval1
if(delegate!=nil){
delegate!.getRespone(time,respones.description)
}
}
}
task.resume()
}
}
class Test{
var tt:NSMutableArray=[]
init(){
for var i=0;i<10;i+=1 {
Thread(target: self, selector: #selector(downloadImages), object: nil)
}
}
void downloadImages(){
for var i=0; i<100;i+=1{
query q=new query()
q.delegate=self;
}
}
void getRespone(time:float,respones:string){
tt.addObject(time)
}
void arrange(){
tt.sortUsingComparator(
{
(s1:AnyObject!,s2:AnyObject!)->NSComparisonResult in
var str1=s1 as float
var str2=s2 as float
if str1<str2{
return NSComparisonResult.OrderedAscending
}else{
return NSComparisonResult.OrderedDescending
}
})
}
}
版权声明: 本文为 InfoQ 作者【phylony-lu】的原创文章。
原文链接:【http://xie.infoq.cn/article/b2aa17bd3ced65f5b9a9c4465】。文章转载请联系作者。
评论