大四实习准备3_java多线程

4.25、27无耻地懒散了。。。。。26号陪女朋友去了。今天28号,继续加油!

2015-4-28

Java 多线程

(java中类不能多继承,可以多层继承;接口则都可以)

定义和创建

方法一:继承Thread类

方法二:实现Runnable接口(以匿名内部类的方式来实现也行)

启动线程

线程只能被启动一次,多次启动线程,即多次调用start方法时,会发生IllegalThreadStateException异常;

//以上综合来个code

 class xianCheng1 extends Thread{
public void run(){
for(int i=0;i<1000;i++)
System.out.println("通过继承Thread定义线程");
}
} class xianCheng2 implements Runnable{
public void run(){
for(int i=0;i<1000;i++)
System.out.println("通过实现Runnable接口定义线程");
}
} public class test{
public static void main(String[] args){
xianCheng1 xc1 = new xianCheng1();//通过继承Thread创建线程对象是比较简单的 xianCheng2 _xc2 = new xianCheng2();
Thread xc2 = new Thread(_xc2);//通过实现Runnable接口创建线程对象就稍微复杂了下 xc1.start();
xc2.start();//是用start()方法,而不是直接调用run()方法,否则就当是普通的函数调用。 new Thread(new Runnable(){ @Override
public void run() {
for(int i=0;i<1000;i++)
System.out.println("通过匿名内部类的方式实现Runnable接口来定义线程");
} }).start();//或者直接通过匿名内部类的方式实现Runnable接口来定义线程
}
}

例1

线程的生命周期:

线程调度:

大四实习准备3_java多线程

(join()方法的状态变化?另外,stop()方法应当也是从运行态变成死亡态)

1.睡眠方法

运行态,调用sleep(time)方法后,进入阻塞态。然后经过time时间后,进入准备态,等待系统调度。再经过一个随机(?应当是的)的时间,重新进入运行态。(从阻塞态到运行态:经过总时间是time+随机)

//函数声明

public static void sleep(long millis)throws InterruptedException;

public static void sleep(long millis,int nanos)throws InterruptedException;

//code必须进行异常处理,否则运行时会发生异常"未报告的异常..."  静态方法,与对象无关

可以通过sleep()方法,实现两个线程交替执行。

 class xianCheng1 extends Thread{
public void run(){
for(int i=0;i<10;i++){
System.out.println("通过继承Thread定义线程");
//新增部分:
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
} class xianCheng2 implements Runnable{
public void run(){
for(int i=0;i<10;i++){
System.out.println("通过实现Runnable接口定义线程");
//新增部分:
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
} public class test{
public static void main(String[] args){
xianCheng1 xc1 = new xianCheng1();//通过继承Thread创建线程对象是比较简单的 xianCheng2 _xc2 = new xianCheng2();
Thread xc2 = new Thread(_xc2);//通过实现Runnable接口创建线程对象就稍微复杂了下 xc1.start();
xc2.start();//是用start()方法,而不是直接调用run()方法,否则就当是普通的函数调用。
}
}

例2

2.线程优先级

从1到10,数字越大、优先级越高。默认的优先级为5。子线程的优先级与父线程相同。

Thread类中有3个表示优先级的常量,MAX_PRIORITY,NORM_PRIORITY,MIN_PRIORITY。

public final void setPriority(int i);

3.让步方法

3.1 yield让步方法 使线程让出当前cpu,进入准备状态,而之后执行哪个线程是不确定的,有系统来选择,有可能还是这个线程。

可以通过yield()方法,实现两个线程交替执行。

public static void yield();

//code 静态方法,与对象无关

 class xianCheng1 extends Thread{
public void run(){
for(int i=0;i<100;i++){
System.out.println("通过继承Thread定义线程");
//改变部分:
Thread.yield();
}
}
} class xianCheng2 implements Runnable{
public void run(){
for(int i=0;i<100;i++){
System.out.println("通过实现Runnable接口定义线程");
//改变部分:
Thread.yield();
}
}
} public class test{
public static void main(String[] args){
xianCheng1 xc1 = new xianCheng1();//通过继承Thread创建线程对象是比较简单的 xianCheng2 _xc2 = new xianCheng2();
Thread xc2 = new Thread(_xc2);//通过实现Runnable接口创建线程对象就稍微复杂了下 xc1.start();
xc2.start();//是用start()方法,而不是直接调用run()方法,否则就当是普通的函数调用。
}
}

例3

3.2 join让步方法 将当前线程的cpu资源让步给指定的线程

//函数声明,及有参数函数的意义?(?认为是执行这么长的时间,然后该线程退出cpu,开始和别的线程并发)

public final void join()throws InterruptedException;

public final void join(long millis)throws InterruptedException;

public final void join(long millis,int nanos)throws InterruptedException;

//code 必须进行异常处理

 class xianCheng1 extends Thread{
public void run(){
for(int i=0;i<1000;i++)
System.out.println("通过继承Thread定义线程");
}
} public class test{
public static void main(String[] args){
xianCheng1 xc1 = new xianCheng1(); xc1.start();
try{
xc1.join();
//xc1.join(1);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("The End");
}
}

例4

上一篇:大四实习准备5_android广播机制


下一篇:jQuery-H5-css3转盘抽奖-遁地龙卷风