20169205 2016-2017-2 《移动平台应用开发实践》第5周学习总结

20169205 2016-2017-2 《移动平台应用开发实践》第5周学习总结

教材学习内容总结

这三章的内容主要是对I/O操作、线程、网络等相关类的具体介绍。
其中以线程和网络这两部分最为重点,有下面几个注意的地方:

  • 线程类主要包括Thread类,Runnable接口,Callable接口等基本类型,下面展示了启动一个线程的方法:(Thread 类实际上实现了Runnable接口)
ThreadClass my = new ThreadClass();
my.start();

Thread类包含一些基本方法:

//当前线程可转让cpu控制权,让别的就绪状态线程运行(切换)
public static Thread.yield() 
//暂停一段时间
public static Thread.sleep()  
//在一个线程中调用other.join(),将等待other执行完后才继续本线程。    
public join()
//后两个函数皆可以被打断
public interrupte()
  • 中断的理解:它并不像stop方法那样会中断一个正在运行的线程。线程会不时地检测中断标识位,以判断线程是否应该被中断(中断标识值是否为true)。终端只会影响到wait状态、sleep状态和join状态。被打断的线程会抛出InterruptedException。Thread.interrupted()检查当前线程是否发生中断,返回boolean。synchronized在获锁的过程中是不能被中断的。

  • java Socket编程技巧
    在java网络编程中,首先需要建立连接,在Java API中以java.net.Socket类的对象代表网络连接,所以建立客户端网络连接,也就是创建Socket类型的对象,该对象代表网络连接,示例如下:

Socket socket1 = new Socket(“192.168.1.111”,8888);
Socket socket2 = new Socket(“www.baidu.com”,80);

上面的代码中,socket1实现的是连接到IP地址是192.168.1.111的计算机的8888号端口,而socket2实现的是连接到域名是www.baidu.com的计算机的80号端口,至于底层网络如何实现建立连接,对于程序员来说是完全透明的。如果建立连接时,本机网络不通,或服务器端程序未开启,则会抛出异常。
接下来的步骤就是按照“请求-响应”模型进行网络数据交换,在Java语言中,数据传输功能由Java IO实现,也就是说只需要从连接中获得输入流和输出流即可,然后将需要发送的数据写入连接对象的输出流中,在发送完成以后从输入流中读取数据即可。示例代码如下:

OutputStream os = socket1.getOutputStream(); //获得输出流
InputStream is = socket1.getInputStream();     //获得输入流

上面的代码中,分别从socket1这个连接对象获得了输出流和输入流对象,在整个网络编程中,后续的数据交换就变成了IO操作,也就是遵循“请求-响应”模型的规定,先向输出流中写入数据,这些数据会被系统发送出去,然后在从输入流中读取服务器端的反馈信息,这样就完成了一次数据交换过程,当然这个数据交换过程可以多次进行。
这里获得的只是最基本的输出流和输入流对象,用流的嵌套将这些获得到的基本流对象转换成需要的装饰流对象,从而方便数据的操作。
最后当数据交换完成以后,关闭网络连接,释放网络连接占用的系统端口和内存等资源,完成网络操作,示例代码如下:

socket1.close();

教材学习中的问题和解决过程

1、java线程中不能抛出异常

错误代码

public class Task implements Runnable {  
  
    @Override  
    public void run() throws Exception {  
        int number = Integer.parseInt("1");  
        throw new Exception("Just for test");  
    }  
}  

错误原因

Java thread不能直接在一个线程里去抛出异常。

解决

一般在线程里碰到checked exception,可行的做法是采用try/catch块来处理。而对于unchecked exception,比较合理的方式是注册一个实现UncaughtExceptionHandler接口的对象实例来处理。

2、java网络编程时报java.net.SocketException: Connection reset异常

错误代码

import java.net.*;
import java.io.*;
public class  Client
{
	public static void main(String[] args) throws Exception 
	{
		Socket socket = new Socket("localhost",8888);
		BufferedOutputStream  out = new BufferedOutputStream(socket.getOutputStream());
		byte[] buff = new byte[2048];
		for(int i = 0 ; i < 2048 ; i ++){
			buff[i] = 'a';
		}
		out.write(buff);
	 	System.out.println("Send Over!");
	}
}

错误原因

客户端在每次读写操作后需要flush语句并且close才能完成。

解决

增加语句

out.flush();
out.close();
socket.close();

上周重点错题总结

1、private void method1(String name) {
if (name.equals("star"))
throw new IllegalArgumentException(name);
}

B .
private void method2(int age) {
if (age > 30)
throw Exception();
}
C .
public double method5() throws Exception {
return 0.7;
}
D .
protected double method4() throws Exception {
throw new Throwable();
}

正确答案: B D 我的答案: A B D

Methods that compile successfully might not be implemented correctly.
This question only asks about the methods that will follow the syntax rules so that they
compile successfully.
Option (a) code compiles successfully. Because IllegalArgumentException is a
runtime exception, method1() can throw it without declaring it to be thrown in its
throws statement.
Option (b) code won’t compile. method2() throws a checked exception, that is,
Exception, without declaring it to be thrown in its throws statement.
Although the code in option. A
method can throw a StackOverflowError (an unchecked exception) without including it in the throws clause of its method declaration.
Option (d) code won’t compile. If a method declares to throw a checked exception, its body can’t throw a more general exception in its body. method4() declares to
throw Exception but throws Throwable, which is not allowed (Exception subclasses
Throwable).
Option (c) code will compile successfully. If a method declares to throw Exception,
it might not actually throw it. This only applies to Exception (because RuntimeException subclasses it), runtime exceptions, and errors

2、Note that March 13, 2016, is the weekend that we spring forward, and November 6, 2016,
is when we fall back for daylight savings time. Which of the following can fill in the blank
without the code throwing an exception?
ZoneId zone = ZoneId.of("US/Eastern");
LocalDate date = ( );
LocalTime time1 = LocalTime.of(2, 15);
ZonedDateTime a = ZonedDateTime.of(date4, time1, zone);

A .
LocalDate.of(2016, 3, 13)
B .
LocalDate.of(2016, 3, 40)
C .
LocalDate.of(2016, 11, 6)
D .
LocalDate.of(2016, 11, 7)
E .
LocalDate.of(2017, 2, 29)
正确答案: A C D 我的答案: C D

Option B is incorrect because there is no March 40th. Option E is incorrect
because 2017 isn’t a leap year and therefore has no February 29th. Option D is correct
because it is just a regular date and has nothing to do with daylight savings time. Options A
and C are correct because Java is smart enough to adjust for daylight savings time.

学习进度条

码云项目地址

代码托管

学习目标

时间 学习时间目标 实际进行时间
第一周 8 8
第二周 10 8.5
第三周 12 10.5
第四周 12 12
第五周 12 11

希望能够通过系统地学习,探索一些开源项目并改进功能,养成良好的开发习惯,形成一套完整的android生态知识体系,并能完全自主开发一款具有一定实用价值的app。

posted @ 2017-03-30 10:45  20169205-lewo  阅读(283)  评论(8编辑  收藏  举报