您的位置:首页 > 其它

API--多线程

2015-12-08 22:21 288 查看
1.创建线程方式:

(1)继承Thread,并重写run方法

public class ThreadDemo01 {
public static void main(String[] args) {
MyThread1 t1=new MyThread1();
MyThread2 t2=new MyThread2();
t1.start();
t2.start();
}
}
class MyThread1 extends Thread{
public void run(){
for(int i=0;i<100;i++){
System.out.println("你好吗?");
}
}
}
class MyThread2 extends Thread{
public void run(){
for(int i=0;i<100;i++){
System.out.println("我很好");
}
}
}


由于java是单继承的,所以在一个项目中,常常 会让一个类去指定扩展一个父类,但若这个类还需要
实现线程的功能,就会导致产生继承冲突。

(2)实现Runnable接口来单独定义线程任务

public class ThreadDemo02 {
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<100;i++){
System.out.println("你是谁?");
}
}
}
class MyRunnable2 implements Runnable{
public void run(){
for(int i=0;i<100;i++){
System.out.println("我是查电表的");
}
}
}


(3)使用匿名内部类-多使用第一种创建线程

public class ThreadDemo03 {
public static void main(String[] args) {
Thread t1=new Thread(){
public void run(){
for(int i=0;i<100;i++){
System.out.println("你是谁?");
}
}
};
t1.start();
Runnable runn=new Runnable(){
public void run(){
for(int i=0;i<100;i++){
System.out.println("我是查水表的");
}
}
};
Thread t2=new Thread(runn);
t2.start();
}
}


2、获取运行该方法的线程

public class ThreadDemo04 {
public static void main(String[] args) {
Thread t1=Thread.currentThread();
System.out.println("运行main方法的线程:"+t1);
}
}


3.设置线程的优先级

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


4.join()方法:使调用该方法的线程进入阻塞状态,直到其等待的线程结束为止。

public class TreadDemo01 {
//模拟是否下载完毕的一个状态
private static boolean isFinish=false;
public static void main(String[] args) {
//错误1:忘记将局部变量写成final,否则内部类不能调用该方法中的局部变量
final Thread download=new Thread(){
public void run(){
System.out.println("download:图片已经开始下载..");
for(int i=0;i<=100;i++){
System.out.println("download:图片下载了"+i+"%");
//错误2:Thread.sleep(50)应该写在写在for循环中,即表示当前线程堵塞50毫秒,(相当于延时)
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}

System.out.println("download:图片已经下载完毕..");
//错误3:忘记将isFinish重置成true
isFinish=true;
}
};
Thread show=new Thread(){
public void run(){
System.out.println("show:图片已经开始显示..");
try {
/*
* 当一个方法的局部内部类中想引用当前方法
* 的其他局部变量,那么该变量必须是final的
*/
download.join();
//    Thread.sleep(50);
} catch (InterruptedException e) {
}

//错误4:忘记判断isFinish,抛出异常状态
if(!isFinish){
throw new RuntimeException("没有图片下载完毕");
}
System.out.println("show:图片已经显示完毕..");
}
};
download.start();
show.start();
}
}


总结:看的懂代码并不代表真的能写代码,上面就是活活生生的例子!!!
6.synchronized块-解决多线程的并发安全问题就是将各干各的变为有顺序的先后做某事。

1 public class SynDemo02 {
2     public static void main(String[] args) {
3         final Shop shop=new Shop();
4         Thread t1=new Thread(){
5             public void run(){
6                 shop.buy();
7             }
8         };
9         Thread t2=new Thread(){
10             public void run(){
11                 shop.buy();
12             }
13         };
14         t1.start();
15         t2.start();
16     }
17 }
18 class Shop{
19     public void buy(){
20         Thread t=Thread.currentThread();
21         System.out.println(t+"正在挑选衣服..");
22         try {
23             Thread.sleep(5000);
24     //错误1:不记得怎么写了
25      //     synchronized(){
26             //锁对象要保证多个线程看到的是同一个才具有通过效果,通常使用this
27             synchronized(this){
28                 System.out.println(t+"正在试衣服..");
29                 Thread.sleep(5000);
30             }
31             System.out.println(t+"结账离开");
32         } catch (InterruptedException e) {
33
34         }
35     }
36 }


synchronized块若声明在方法上,那么上锁的对象就是当前方法所属的对象(this)

互斥锁:当使用synchronized为两段不同的代码上锁,而多个线程看到的是相同的锁对象时,那么,这两段代码间就具有了互斥效果(多个线程间不能同时进入到 两个方法内部)

public class SyncDemo03 {
public static void main(String[] args) {
final Foo foo=new Foo();
Thread t1=new Thread(){
public void run(){
foo.methodA();
}
};
Thread t2=new Thread(){
public void run(){
foo.methodB();
}
};
t1.start();
t2.start();
}

}
class Foo{
public synchronized void methodA(){
try {
Thread t1=Thread.currentThread();
System.out.println(t1+"正在调用A方法");
Thread.sleep(5000);
System.out.println("运行A方法结束");
} catch (InterruptedException e) {

}
}
public synchronized void methodB(){
try {
Thread t1=Thread.currentThread();
System.out.println(t1+"正在调用B方法");
Thread.sleep(5000);
System.out.println("运行B方法结束");
} catch (InterruptedException e) {
}
}
}


7.Object定义了两个方法wait()和notify()
(1)wait方法可以阻塞调用该方法的线程,直到该对象的
(2) notify方法被调用,才能解除线程的阻塞状态。

private static Object obj = new Object();
synchronized (obj) {
obj.notify();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: