java多线程与并发库课件,Java多线程与并发库的简单应用

一、创建线程的方式

package cn.itcast.heima2;

public class TraditionalThread {

public static void main(String[] args) {

// TODO Auto-generated method stub

Thread thread = new Thread() {

@Override

public void run() {

while (true) {

try {

Thread.sleep(500);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName());

System.out.println(this.getName());

}

}

};

thread.start();

Thread thread2 = new Thread(new Runnable() {

@Override

public void run() {

while (true) {

try {

Thread.sleep(500);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName());

}

}

});

thread2.start();//更可以体现面向对象的思想,线程和代码隔离

new Thread(new Runnable() {

@Override

public void run() {

while (true) {

try {

Thread.sleep(500);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("runnable:"+Thread.currentThread().getName());

}

}

}){

@Override

public void run() {

while (true) {

try {

Thread.sleep(500);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("thread:"+Thread.currentThread().getName());

}

}

}.start();

}

}

二、定时器

代码1:

package cn.itcast.heima2;

import java.util.Calendar;

import java.util.Timer;

import java.util.TimerTask;

public class TraditionalTimerTest {

public static void main(String[] args) {

new Timer().schedule(new TimerTask() {

@Override

public void run() {

System.out.println("OUTER:boom!");

}

},10000,3000);

while(true){

System.out.println(Calendar.getInstance().get(Calendar.SECOND));

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

代码2:

package cn.itcast.heima2;

import java.util.Calendar;

import java.util.Timer;

import java.util.TimerTask;

public class TraditionalTimerTest {

static class MyTimerTask extends TimerTask{

static int count=0;

@Override

public void run() {

// TODO Auto-generated method stub

count=(count+1)%2;

System.out.println("OUTER:boom!");

new Timer().schedule(new MyTimerTask(), 2000+2000*count);

}

}

public static void main(String[] args) {

new Timer().schedule(new MyTimerTask(),2000);

while(true){

System.out.println(Calendar.getInstance().get(Calendar.SECOND));

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

三、线程同步通信技术

子线程10次,然后主线程100次,然后子线程10次,然后主线程100次。循环50次

package cn.itcast.heima2;

public class TraditionalThreadComunication {

public static void main(String[] args) {

// TODO Auto-generated method stub

Business business = new TraditionalThreadComunication().new Business();

new Thread(new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

for (int i = 0; i < 50; i++) {

business.sub(i);

}

}

}).start();

for (int i = 0; i < 50; i++) {

business.main(i);

}

}

class Business {

private boolean bShouldSub=true;

public synchronized void sub(int i) {

if(!bShouldSub){

try {

this.wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

for (int j = 0; j < 10; j++) {

System.out.println("sub thread sequence:" + j + " loop of " + i);

}

bShouldSub=false;

this.notify();

}

public synchronized void main(int i) {

if(bShouldSub){

try {

this.wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

for (int j = 0; j < 100; j++) {

System.out.println("main thread sequence:" + j + " loop of " + i);

}

bShouldSub=true;

this.notify();

}

}

}

四、线程范围内共享变量的概念与作用

如下代码中会出现问题

package cn.itcast.heima2;

import java.util.HashMap;

import java.util.Random;

public class ThreadScopeShareData {

private static int data = 0;

private static HashMap threadData = new HashMap<>();

public static void main(String[] args) {

for (int i = 0; i < 2; i++) {

new Thread(new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

data = new Random(System.currentTimeMillis()).nextInt();

System.out.println(Thread.currentThread().getName() + " has put data :" + data);

threadData.put(Thread.currentThread(), data);

new A().get();

new B().get();

}

}).start();

}

}

static class A {

public void get() {

int data=threadData.get(Thread.currentThread());

System.out.println("A from " + Thread.currentThread().getName() + " has put data :" + data);

}

}

static class B {

public void get() {

int data=threadData.get(Thread.currentThread());

System.out.println("B from " + Thread.currentThread().getName() + " has put data :" + data);

}

}

}

解决方法:

package cn.itcast.heima2;

import java.util.HashMap;

import java.util.Random;

public class ThreadScopeShareData {

private static int data = 0;

static long[] seed = new long[] { 12345612, 654321 };

private static HashMap threadData = new HashMap<>();

public static void main(String[] args) {

new Thread(new ThreadScopeShareData.MyRunnable() {

@Override

public void run() {

// TODO Auto-generated method stub

data = MyRunnable.rd.nextInt();

System.out.println(Thread.currentThread().getName() + " has put data :" + data);

threadData.put(Thread.currentThread(), data);

new A().get();

new B().get();

}

}).start();

new Thread(new ThreadScopeShareData.MyRunnable() {

@Override

public void run() {

// TODO Auto-generated method stub

data = MyRunnable.rd.nextInt();

System.out.println(Thread.currentThread().getName() + " has put data :" + data);

threadData.put(Thread.currentThread(), data);

new A().get();

new B().get();

}

}).start();

}

static class A {

public void get() {

int data=threadData.get(Thread.currentThread());

System.out.println("A from " + Thread.currentThread().getName() + " has put data :" + data);

}

}

static class B {

public void get() {

int data=threadData.get(Thread.currentThread());

System.out.println("B from " + Thread.currentThread().getName() + " has put data :" + data);

}

}

abstract static class MyRunnable implements Runnable {

static Random rd = new Random();

public abstract void run();

}

}

五、读写锁技术的妙用

一个线程在写的时候其他线程都不能打断写入过程,就是写入的时候不能读取

package cn.itcast.heima2;

import java.util.Random;

import java.util.concurrent.locks.ReadWriteLock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLockTest {

static Random rd = new Random();

public static void main(String[] args) {

final Queue3 q3 = new Queue3();

for (int i = 0; i < 3; i++) {

new Thread() {

public void run() {

while (true) {

q3.get();

}

}

}.start();

new Thread() {

public void run() {

while (true) {

q3.put(rd.nextInt(10000));

}

}

}.start();

}

}

}

class Queue3 {

private Object data = null;// 共享数据,只能有一个线程能写该数据,但可以有多个线程同时读该数据。

ReadWriteLock rwl = new ReentrantReadWriteLock();

public void get() {

rwl.readLock().lock();

try {

System.out.println(Thread.currentThread().getName() + " be ready to read data!");

Thread.sleep((long) (Math.random() * 1000));

System.out.println(Thread.currentThread().getName() + " have read data :" + data);

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

rwl.readLock().unlock();

}

}

public void put(Object data) {

rwl.writeLock().lock();

try {

System.out.println(Thread.currentThread().getName() + " be ready to write data!");

Thread.sleep((long) (Math.random() * 1000));

this.data = data;

System.out.println(Thread.currentThread().getName() + " have write data: " + data);

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

rwl.writeLock().unlock();

}

}

}

package cn.itcast.heima2;

import java.util.HashMap;

import java.util.Map;

import java.util.concurrent.locks.ReadWriteLock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class CacheDemo {

private Map cache = new HashMap();

public static void main(String[] args) {

// TODO Auto-generated method stub

}

private ReadWriteLock rwl = new ReentrantReadWriteLock();

public Object getData(String key){

rwl.readLock().lock();

Object value = null;

try{

value = cache.get(key);

if(value == null){

rwl.readLock().unlock();

rwl.writeLock().lock();//A

try{

if(value==null){//如果有多个线程同时到达A点了,那这个数据要写入多次,这是不对的

value = "aaaa";//实际失去queryDB();

}

}finally{

rwl.writeLock().unlock();

}

rwl.readLock().lock();

}

}finally{

rwl.readLock().unlock();

}

return value;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值