/**
*根据javaAPI_1.7中Thread类,有两种创建新进程的方法
*一种方法是将类声明为 Thread 的子类。该子类应重写 Thread 类的 run 方法。接下来可以分配并启动该子类的实例。
*/
操作一:在T中建一个线程用来在主线程中进行调用
源码:
public class T{
class Thread1 extends Thread{重写run}
public void main(String[]ss){调用start//一个够了}
}
报错:No enclosing instance of type T(类名) is accessible. Must qualify the allocation with an enclosing instance of type T(类名)(e.g. x.new A() where x is an instance of T(类名)).
翻译:不存在可用的T类的内部实例。必须配置T的内部实例。如x.new A(),x必须是T的实例
操作二:为解决一中问题,将Thread1t移到T外
源码:
public class T{主线程main}
public class Thread1 extends Thread{}
提示:public type thread1 must be difined in its own file.
(去除public)succeed!
---------------------------------------------------------------------------
/**
*另一种方法是声明实现 Runnable 接口的类。
*该类然后实现 run 方法。
*然后可以分配该类的实例,在创建 Thread 时作为一个参数来传递并启动
*/
class Thread2 implements Runnable{实现run方法}
class T {调用run}
succeed!
---------------------------------------------------------------------------