您的位置:首页 > 其它

Thread1(多线程 )太多了还是分两节吧,你们原谅我.....

2017-09-09 14:53 761 查看
多线程的小结:

1.创建方式3种
1.继承Thread并重写run方法来定义线程要执行的任务

缺点:1.java单继承无法继承其他类

   2. run方法规定了线程执行的任务,线程与任务有强耦合关系,不利于线程的重用。

2.实现Runnable接口并重写run方法

3.使用匿名内部类的形式创建线程

2.run()是要执行的任务,start()是启动线程,进入runnable,一旦获取cpu时间,run方法会自动被调用

3.获取运行当前方法的线程,以及获取该线程的信息(id,name,priority,isAlive isDaemon)5个

Thread main=Thread.currentThread();

5.线程的优先级:线程的时间片分配完全听线程调度的,线程只能被动分配时间,对于线程调度的工作不能干预,可以通过提高优先级来达到尽可能干预的目的,理论上优先级越高,获取cpu时间片的次数越多

6.静态方法sleep()线程能够进入阻塞状态指定毫秒,超时回到Runnable状态.

7.前台线程结束,后台线程(守护线程)强制结束。

8.join方法使线程进入阻塞状态直到其他线程完成工作才会停止阻塞,用于多个线程之间的同步工作协调

********************************************************************************************

第一种方式:耦合度太强,不好

Thread t1=new MyThread1();
Thread t2=new MyThread2();
t1.start();
t2.start();
class MyThread1 extends Thread{
public void run(){
for(int i=0;i<10000;i++){
System.out.println("你是谁呀?");
}
}
}
class MyThread2 extends Thread{
public void run(){
for(int i=0;i<10000;i++){
System.out.println("我是查水表的!");
}
}
}

*******************************************************************************************************************

第二种方式:不错,耦合度少了

public class demo2 {
public static void main(String[] args) {
Runnable r1=new MyRunnable1();
Runnable r2=new MyRunnable2();
Thread t1=new Thread(r1);
Thread t2=new Thread(r2);
t1.start();
t2.start();
}

}

class MyRunnable1 implements Runnable{
public void run() {
for(int i=0;i<10000;i++){
System.out.println("你是谁呀?");
}
}

}

class MyRunnable2 implements Runnable{
public void run(){
for(int i=0;i<10000;i++){
System.out.println("我是查水表的!");
}
}

}

********************************************************************************************************************************************************************

第三种方式:很好,代码量少了....

public class demo3 {
public static void main(String[] args) {
new Thread(){
public void run(){
for(int i=0;i<10000;i++){
System.out.println("你是谁呀");
}
}
}.start();
//方式2
new Thread(new Runnable(){
public void run() {
for(int i=0;i<10000;i++){
System.out.println("我是查水表的!");
}
}

}).start();
}

}

**************************************************************************************************

比较经典的方式获取运行该方法的线程

public class demo4 {
public static void main(String[] args) {
Thread main=Thread.currentThread();
System.out.println(main);
dosome();
Thread t=new Thread(){
public void run(){
Thread t=Thread.currentThread();
System.out.println(t);
dosome();
}
};
t.start();
}
public static void dosome(){
Thread t=Thread.currentThread();
System.out.println(t);
}

}

*********************************************************************************

public class demo5 {
public static void main(String[] args) {
//获取main方法的线程
Thread main=Thread.currentThread();
long id=main.getId();
System.out.println(id);

String name=main.getName();
System.out.println(name);

int priority=main.getPriority();
System.out.println(priority);

boolean isAlive=main.isAlive();
System.out.println(isAlive);

boolean isDaemon=main.isDaemon();
System.out.println(isDaemon);
}

}

*******************************************************************************************************************

public class demo6 {
public static void main(String[] args) {
Thread min=new Thread(){
public void run(){
for(int i=0;i<10000;i++){
System.out.println("min");
}
}
};
Thread norm=new Thread(){
public void run(){
for(int i=0;i<10000;i++){
System.out.println("norm");
}
}
};
Thread max=new Thread(){
public void run(){
for(int i=0;i<10000;i++){
System.out.println("max");
}
}
};
min.setPriority(Thread.MIN_PRIORITY);
norm.setPriority(Thread.NORM_PRIORITY);
max.setPriority(Thread.MAX_PRIORITY);
min.start();
norm.start();
max.start();
}

}

********************************************************************************************************

public class demo7 {
public static void main(String[] args) {
while(true){
clock();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {

}
}
}
public static void clock(){
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
String str=sdf.format(date);
System.out.println(str);
}

}

********************************************************************************************************

public class demo8 {
public static void main(String[] args) {
Thread rose=new Thread(){
public void run(){
for(int i=0;i<5;i++){
System.out.println("let  me  go  !");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
System.out.println("啊啊啊啊");
}
};
Thread jack=new Thread(){
public void run(){
while(true){
System.out.println(" you jump, i jump!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}

};
//设置后台线程
jack.setDaemon(true);
rose.start();
jack.start();
}

}

*********************************************************************************************

public class demo9 {
//表示图片是否下载完毕
public static boolean isFinish =false;

public static void main(String[] args) {
Thread download=new Thread(){
public void run(){
System.out.println("图片开始下载!");
for(int i=0;i<100;i++){
System.out.println("down:"+i+"%");
try {
Thread.sleep(50);
} catch (InterruptedException e) {

}
}
System.out.println("图片下载完毕!");
isFinish=true;
}
};
Thread show=new Thread(){
public void run(){
System.out.println("图片开始显示!");
   //先等待download把图片下载完毕!
try {
download.join();
} catch (InterruptedException e) {

}
if(!isFinish){
throw new RuntimeException("图片没有下载完毕!");
}
System.out.println("图片显示完毕!");
}
};
download.start();
show.start();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: