您的位置:首页 > 职场人生

黑马程序员_Java基础_多线程

2015-04-28 21:25 393 查看
------- android培训java培训、期待与您交流! ----------

 一.主线程是负责Java程序执行,它存在与mian方法中

 *  jvm启动不止一个线程,还有负责垃圾回收机制的线程

 *

 * Thread类是java对线程的封装,API中说明只需要继承这个类就能实现一个线程。

 *

 * 创建线程的第一种方式,就是继承Thread类

 * 步骤:1.定义类继承Thread

 *                 2.重写Thread类中的run方法

 *                         目的:将自定义的代码存储到run方法中,让线程运行

 *

 *               3.调用线程的start方法(作用:启动线程,调用run方法)

 创建线程的第二种方式:实现Runnable接口

 步骤

      1,定义类实现Runnable接口

      2,覆盖Runnable接口中的run方法

          将线程要运行的代码放到该run方法中

      3,通过Thread类建立线程对象

      4,将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数(为了放线程去指定对象的run方法)

      5,调用Thread类的start方法开启线程,并调用Runnable接口子类的run方法

 实现方式和继承方式有什么区别?

 实现方式好处:避免了单继承的局限性,我们建议使用实现方式进行定义线程。

 区别:   继承Thread:线程代码存放在Thread子类的run方法中

 实现Runnable:线程代码存放在接口的子类的run方法中。

 

 * 通过结果:运行的结果每次都是不同的。

 *                         因为多个线程都在获取cpu的执行权,cpu执行到谁,就运行谁。

 *

 * 结论:cpu执行过程中的某一时刻,只能有一个程序在运行(多核除外)

 *                 执行时间由cpu决定。

 *   

 * 多线程的特性:随机性。

 *

 * 常用方法:static Thread currentThread()获取当前线程对象

 *                  getName();获取线程名称

 *

 * 设置线程名称:setName或者构造函数

 多线程安全问题:常见问题,当多条语句在操作同一个线程共享数据是,一个线程对多条语句只执行了一部分

 还没执行完,另外一个线程参与进来执行,导致共享数据的错误。

 解决办法:

 对多条操作共享数据的语句,只能让一个线程都执行万,在执行过程中,其他线程不可以参与。

 Java对于多线程的安全问题提供了专业的解决方式(同步代码):

 synchronized(对象){

   需要同步的代码;

 }

 对象如同锁,持有锁的线程可以在同步中执行,没有锁的线程即使得到cpu执行权,也进不去,因为没有获取锁

 如何停止线程?

 方法:只有一种方法,run方法结束。(stop方法已经过时)

 开启多线程运行,运行代码通常是循环结构

 要让线程结束只有控制循环

 特殊情况:

 当线程处于冻结状态

 就不会读取到标记,那么线程就不会结束

 

例子1:

class Demo extends Thread {

public Demo(String name) {
super(name); //用于标识线程的名称
}
public void run() {
for (int i = 0; i <= 60; i++) {
System.out.println(Thread.currentThread().getName()+"Demo run " + i);
}
}
}

public class ThreadDemo {

public static void main(String[] args) {
Demo d = new Demo("one-----"); //创建一个进程
Demo b = new Demo("two-----"); //创建一个进程
d.start();//开启线程并执行该线程的run方法
b.start();
// d.run(); 仅仅是对象调用方法,而线程创建了,并没有使用

for (int i = 0; i <= 60; i++) {
System.out.println("Mian run " + i);
}

}

}


 同步的前提:

 1,必须要有两个或者两个以上的线程。

 2,必须是多个线程使用同一个锁

 

同步好处:解决了多线程的安全问题

同步弊端:多个线程需要判断锁,比较消耗资源

例子2:

 需求:一个售票小程序

class Ticket implements Runnable {
private static int tick = 100;
Object obj = new Object();
@Override
public void run() {
while (true) { // 注意 这里会把资源耗尽的 小心哦
synchronized (obj) {
if (tick > 0) {
try {
Thread.sleep(10); // 休眠10毫秒
} catch (InterruptedException e) {
}
System.out.println(Thread.currentThread().getName()
+ "sale*********" + tick--);
}
}
}
}

}

public class TicketDemo {

/**
* @param args
*/
public static void main(String[] args) {

// 开启四个售票窗口
Ticket tick = new Ticket();

Thread t1 = new Thread(tick);
Thread t2 = new Thread(tick);
Thread t3 = new Thread(tick);
Thread t4 = new Thread(tick);
t1.start();
t2.start();
t3.start();
t4.start();
}

}


例子3:

/*
需求:银行存钱,有两个用户分别存入300元,每次存入100,存3次

多线程安全:多线程使用共享数据时,必须使用同步锁,防止出现安全问题(synchronized)

静态的同步方法:使用的锁是该方法所在类的字节码文件对象。类名.class
*/

class Bank {
private int sum;

public synchronized void add(int n) {   //同步,使得多线程安全
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
sum = sum + n;
System.out.println(Thread.currentThread().getName() + "---->sum:" + sum);
}
}

class Cus implements Runnable{

private Bank b = new Bank();

@Override
public void run() {
for(int i =0 ; i<3 ;i++){
b.add(100);
}

}

}

public class BankDemo {
public static void main(String[] args) {

Cus c = new Cus();

//添加两个线程
Thread t1 = new Thread(c);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}

}


例子4:

/*
* 需求:两个用户,一个往数据库中加入数据,一个向数据库中申请读出数据
*
* 多线程通讯:要解决线程安全问题,使用到等待唤醒机制
*
* wait();
* notify();
* notifyall();
* 都是使用在同步中,因为要对持有监视器(锁)的线程操作
* 所以要使用在同步中,因为持有同步才具有锁
*
* 等待和唤醒必须是同一个锁
* 而锁可以是任意对象,所有可以被任意对象调用的方法定义Object类中
*/

class Res{
String name;
String sex;
boolean flag = false;
}
class Input implements Runnable{
private Res r;
public Input(Res r) {
this.r = r;
}
@Override
public void run() {
int x= 0;
while (true) {
synchronized (r) {
if(r.flag)
try {
r.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(x == 0)
{
r.name="make";
r.sex="man";
}
else{
r.name="丽丽";
r.sex="女女女女女女";
}
x=(x+1)%2;
r.flag=true;
r.notify();

}
}
}
}

class Output implements Runnable{
private Res r;
Output(Res r){
this.r= r;
}
public void run(){
while (true) {
synchronized (r) {
if(!r.flag)
try {
r.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStack
4000
Trace();
}
System.out.println(r.name + "........." + r.sex);
r.flag=false;
r.notify();
}
}

}
}

public class InputOutputDemo {

/**
* @param args
*/
public static void main(String[] args) {

Res r = new Res();
Input in = new Input(r);
Output out = new Output(r);

Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}

}


例子5:

守护线程:当主线程执行完毕时,只剩下守护线程是,java 虚拟机会自动退出。

class StopThread implements Runnable {
private boolean flag = true;

@Override
public synchronized void run() {

while (flag) {
try {
this.wait();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()
+ "++++Exception-----");
flag = false;
}
System.out.println(Thread.currentThread().getName() + "run------");
}
}

public void changeFlag() {
flag = false;
}
}

public class StopThreadDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StopThread s = new StopThread();

Thread t1 = new Thread(s);
Thread t2 = new Thread(s);
t1.setDaemon(true); //标记线程为守护线程,必须在启动线程之前调用
t2.setDaemon(true);
t1.start();
t2.start();

int num = 0;
while (true) {
if (num++ == 60) {
// s.changeFlag();
t1.currentThread(); // 线程中断:就是使处在冻结状态的线程回到运行状态
t2.currentThread();
break;
}
System.out.println(Thread.currentThread().getName() + "--main-----"+num);
}
System.out.println("over");
}

}




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