黑马程序员----多线程

[align=center][size=medium][b]多线程[/b] [/size][/align]
[align=center]---------------------- [url=http://edu.csdn.net/heima]android培训[/url]、[url=http://edu.csdn.net/heima]java培训[/url]、期待与您交流! ---------------------- [/align]


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()之前设置.
对应代码:

[quote]public class Demo8 {

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


Thread t = new Thread(new Runnable(){
public void run(){
for(int x = 0;x<10;x++){
System.out.println("x = "+x);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});



for(int m = 0;m<5;m++){
System.out.println("m = "+m);
Thread.sleep(1000);
}


这四行代码房子for前与for后结果不一样
t.setDaemon(true);
th.setDaemon(true);
t.start();
th.start();

}

}

class ThTest extends 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 block
e.printStackTrace();
}
}
}
}[/quote] join(), 当前线程暂停, 加入一个线程, 等待这个线程执行结束之后, 当前线程继续
对应代码:
package cn.thread;

public class Demo9 {

/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub

Thread 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 block
e.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 stub
final 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 stub
final 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 block
e.printStackTrace();
}
}
for(int i = 0;i<3;i++){
System.out.println(i);

}
System.out.println("------1----");
num = 2;
c1.signal();//指定唤醒c1
lo.unlock();
}

public void p2(){
lo.lock();
if(num != 2){
try {
c1.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int i = 0;i<5;i++){
System.out.println(i);

}
System.out.println("------2----");
num = 1;
c.signal();//指定唤醒c2
lo.unlock();
}
}



[align=center]---------------------- [url=http://edu.csdn.net/heima]android培训[/url]、[url=http://edu.csdn.net/heima]java培训[/url]、期待与您交流! ---------------------- 详细请查看:[url=http://edu.csdn.net/heima]http://edu.csdn.net/heima[/url][/align]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值