首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

黑马软件工程师-多线程

2012-09-20 
黑马程序员----多线程多线程 ---------------------- android培训、java培训、期待与您交流! --------------

黑马程序员----多线程
多线程
---------------------- android培训、java培训、期待与您交流! ----------------------


1.什么是线程
线程就是程序执行时的一条路径
* 2.创建线程
定义类, 继承Thread, 重写run()方法, 创建该类对象, 调用start(), 程序就会开启新线程执行run()方法
定义类, 实现Runnable, 重写run()方法, 创建该类对象, 传入Thread类构造函数, 在Thread对象调用start()方法时, 开启新线程运行run()方法
对应代码:
//两种写法

public class Demo1 {public static void main(String[] args) {Thread t = new Thread(){public void run(){for(int i = 0;i<10;i++){System.out.println("i = "+i);}}};t.start();new Thread(new Runnable(){public void run(){for(int x = 0; x < 10;x++){System.out.println("x = "+x);}}}).start();}}
3.Thread类常用方法
currentThread(), 获取当前的线程对象
getName(), setName(String), 获取和设置线程的名字
sleep(long), 控制线程休眠指定毫秒
setDaemon(boolean), 设置线程为守护线程, 不会单独运行. 守护线程必须在线程调用start()之前设置.
对应代码:

package cn.thread;public class Demo9 {/** * @param args * @throws InterruptedException */public static void main(String[] args) throws InterruptedException {// TODO Auto-generated method stubThread t = new Thread(){public void run(){for(int i = 0;i < 10;i++){System.out.println("i = "+i);try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}};t.start();for(int m = 0;m<10;m++){System.out.println("m = "+m);Thread.sleep(1000);if(m == 4){//当m = 4的时候m暂停,i运行到结束后,m才继续运行t.join();}}}}4.多线程同步
如果在多线程并发的时候, 我们希望某一段代码执行过程中不被其他线程打断, 那么就可以使用同步技术
同步的方式有两种:
a.同步代码块:
synchronized(锁对象){同步代码}, 多个线程在执行同步代码块时如果锁对象相同, 只能执行一个
对应代码:
package cn.thread;public class Demo3 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubfinal Happ h = new Happ();new Thread(new Runnable(){public void run(){while(true){h.happ();}}}).start();new Thread(new Runnable(){public void run(){while(true){h.toHappy();}}}).start();}}class Happ{private Object lock = new Object(); public void happ(){synchronized(lock){System.out.print("圣");System.out.print("诞");System.out.print("快");System.out.println("乐");}}public void toHappy(){synchronized(lock){System.out.print("元");System.out.print("旦");System.out.print("快");System.out.println("乐");}}}

b.同步方法:
在方法签名上加入synchronized修饰, 那么整个方法都被同步, 使用的锁对象是this
package cn.thread;public class Demo3 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubfinal Happ h = new Happ();new Thread(new Runnable() {public void run() {while (true) {h.happ();}}}).start();new Thread(new Runnable() {public void run() {while (true) {h.toHappy();}}}).start();}}class Happ {public synchronized void happ() {System.out.print("圣");System.out.print("诞");System.out.print("快");System.out.println("乐");}public synchronized void toHappy() {System.out.print("元");System.out.print("旦");System.out.print("快");System.out.println("乐");}}

多个线程并发执行, 访问同一数据时有可能出现线程安全问题, 可以使用同步技术解决.
5.多线程通信
在多线程同步的时候, 可以使用锁对象控制当前线程等待或者唤醒其他等待的线程
使用锁对象.wait()方法可以控制当前线程等待
使用锁对象.notify()方法可以唤醒随机一个在等待的线程
使用锁对象.notifyAll()方法可以唤醒所有在等待的线程
6.JDK5的同步和通信
同步使用ReentrantLock类创建对象, lock()方法开始同步, unlock()方法结束同步
通信使用ReentrantLock类的newCondition()方法可以获取一个Condition对象, 使用该对象的await()和signal()以及signalAll()方法进行通信
对应代码:
package cn.thread;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class Demo11 {public static void main(String[] args){final pTest p = new pTest();new Thread(new Runnable(){public void run(){for(int j = 0;j<5;j++){p.p1();}}}).start();new Thread(new Runnable(){public void run(){for(int j = 0;j<5;j++){p.p2();}}}).start();}}class pTest{private ReentrantLock lo = new ReentrantLock();private Condition c = lo.newCondition();private Condition c1 = lo.newCondition();private int num = 1;public void p1(){lo.lock();if(num != 1){try {c.await();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}for(int i = 0;i<3;i++){System.out.println(i);}System.out.println("------1----");num = 2;c1.signal();//指定唤醒c1lo.unlock();}public void p2(){lo.lock();if(num != 2){try {c1.await();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}for(int i = 0;i<5;i++){System.out.println(i);}System.out.println("------2----");num = 1;c.signal();//指定唤醒c2lo.unlock();}}



---------------------- android培训、java培训、期待与您交流! ---------------------- 详细请查看:http://edu.csdn.net/heima

热点排行