发表于: 2016-07-03 00:07:34
1 2227
今天完成的事情:最近学习了容器、IO、线程的相关概念以及用法
明天计划的事情:之后继续学习GUI,网络等,java基础就算是告一段落了(感觉学的不是很扎实)
遇到的问题:下面创建线程的这段代码中,疑问有二:
一、 不考虑扩展等后期,继承Thread和实现Runnable有和本质区别(如内存方面、运行结果)
二、实现Interface的绿色部分,传不传对象的方法都有,而且结果各异,比较苦恼本质区别在哪
public class TT implements Runnable {
int b = 100;
public static void main(String[] args) throws Exception {
TT tt = new TT();
Thread t = new Thread(tt);
t.start();
tt.m2();System.out.println(tt.b);
}
public synchronized void m1() throws Exception{
b = 1000;Thread.sleep(5000);System.out.println("b = " + b);}
public synchronized void m2() throws Exception {
Thread.sleep(2500);b = 2000;}
public void run() {
try {m1();} catch(Exception e) {e.printStackTrace();}}}
评论