1,包及控制访问权限
如果某个类中的方法全是static类型的,导入的时候就要静态导入,import static 包.类.*
private--default--protected----public
所有类的首字母大写,方法第一个单词首字母小写,之后的单词首字母大写,属性同上,包名全部小写。常量全部大写
2,多线程
进程包括很多线程.
一个类只有继承了Thread类,此类才可以被称为多线程实现类。并且必须明确覆写Thread类中的run方法,此方法位线程的主体。
</pre>这是两个线程交错执行<pre name="code" class="java">package com.itheima;
class MyThread extends Thread{
private String name;
public MyThread(String name){
this.name = name;
}
public void run(){
for(int i=0;i<10;i++){
System.out.println(name+"运行"+i);
}
}
}
public class demo6 {
public static void main(String[] args) throws Exception{
MyThread a = new MyThread("线程A");
MyThread b = new MyThread("线程B");
a.start();//a只可以调用一次start
b.start();
}
}
结果:
线程A运行0
线程B运行0
线程A运行1
线程B运行1
线程A运行2
线程B运行2
线程A运行3
线程B运行3
线程A运行4
线程B运行4
线程A运行5
线程B运行5
线程A运行6
线程B运行6
线程A运行7
线程B运行7
线程A运行8
线程B运行8
线程A运行9
线程B运行9
之所以线程启动不是使用run,是因为线程的启动需要底层的操作系统的支持。在start方法中,实际上调用了Start0,这个方法是native的,表示调用本机操作系统也可以通过实现Runnable接口来实现多线程
package com.itheima;
class MyThread implements Runnable{
private String name;
public MyThread(String name){
this.name = name;
}
public void run(){
for(int i=0;i<10;i++){
System.out.println(name+"运行"+i);
}
}
}
public class demo6 {
public static void main(String[] args) throws Exception{
MyThread a = new MyThread("线程A");
MyThread b = new MyThread("线程B");
Thread a1 = new Thread(a);
Thread b1 = new Thread(b);
a1.start();
b1.start();
}
}
线程A运行0
线程B运行0
线程A运行1
线程B运行1
线程A运行2
线程B运行2
线程A运行3
线程B运行3
线程A运行4
线程B运行4
线程A运行5
线程B运行5
线程A运行6
线程B运行6
线程A运行7
线程B运行7
线程A运行8
线程B运行8
线程A运行9
线程B运行9
不管怎么样,都需要依靠Thread类才可以启动多线程
Public clas Thread extends Object implements Runnable
如果一个类继承Thread不适合资源共享,实现Runnable适合资源共享
线程的状态:创建,就绪,运行,阻塞,终止.