一,传统创建县城的两种方式:
public static void main(String[] args) throws InterruptedException {
// 创建线程的方式一:
Thread thread1 = new Thread() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread1:"+Thread.currentThread().getName());
}
}
};
thread1.start();
// 创建线程的方式二:
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
System.out.println("thread2:"+Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread2.start();
}
二,定时器的使用:
public static void main(String[] args) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
System.out.println("bombing...");
}
}, 1000,3000);//1秒以后每隔3秒的时间执行定时任务
}
三,线程之间的互斥:
要求,写出符合一下要求的程序代码:子线程循环50次,主线程循环10,如此循环50次:
package com.adam.j2se.thread;
public class TriditionalThreadCommucation {
public static void main(String[] args) throws InterruptedException {
final Business business = new Business();
for (int i = 0; i < 50; i++) {
new Thread(new Runnable() {
public void run() {
try {
business.subThread();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
business.mainThread();
}
}
}
class Business {
boolean bShouldSub = true;
public synchronized void subThread() throws InterruptedException {
while (bShouldSub) {
for (int i = 1; i <= 50; i++) {
System.out.println("sub thread loop " + i + " of: 50");
}
bShouldSub = false;
this.notify();
}
this.wait();
}
public synchronized void mainThread() throws InterruptedException {
while (!bShouldSub) {
for (int i = 1; i <= 10; i++) {
System.out.println("main thread loop " + i + " of :100");
}
bShouldSub = true;
this.notify();
}
this.wait();
}
}
三,线程范围内共享数据:
package com.adam.j2se.thread;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
public class ThreadScopeSharedData {
private static Map<Thread, Integer> threadScope = new HashMap<Thread, Integer>();
// private static ThreadLocal<Integer> local = new ThreadLocal<Integer>();
// private static int data = 0;
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 2; i++) {
new Thread(new Runnable() {
@Override
public void run() {
int data = new Random().nextInt();
/*
* System.out.println(Thread.currentThread().getName() +
* " put data is:" + data);
*/
// local.set(data);
/* threadScope.put(Thread.currentThread(), data); */
ThreadData instance = ThreadData.getThreadInstance();
instance.setName("xieqiang" + UUID.randomUUID());
instance.setAge(new Random().nextInt());
System.out.println("put data:"+Thread.currentThread().getName()+instance);
new A().getData();
new B().getData();
}
}).start();
}
}
static class A {
public void getData() {
System.out.println(Thread.currentThread().getName() + ThreadData.getThreadInstance().toString());
}
}
static class B {
public void getData() {
System.out.println(Thread.currentThread().getName() + ThreadData.getThreadInstance().toString());
}
}
}
/**
* 线程安全的单类模式
*
* @author xieqiang
* 对单模式进行改造,了用ThreadLocal使得到的instance与当前线程进行绑定,以确保在线程范围内共享一个instance;
*
*/
class ThreadData {
private ThreadData() {
}
private static ThreadLocal<ThreadData> map = new ThreadLocal<ThreadData>();
public static ThreadData getThreadInstance() {
ThreadData data = map.get();
if (data == null) {
ThreadData data2 = new ThreadData();
map.set(data2);
return data2;
}
return data;
}
private String name;
private Integer age;
public String getName() {
return name;
}
@Override
public String toString() {
return "ThreadData [name=" + name + ", age=" + age + "]";
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
四,多线程共享数据:
1,
package com.adam.j2se.thread;
public class MultiThreadSharedData {
public static void main(String[] args) throws InterruptedException {
BusinessData data = new BusinessData();
new Thread(data).start();
new Thread(data).start();
Thread.sleep(1000);
System.out.println(BusinessData.count);
}
}
//多线程共享数据方式一:若多个线程对数据有相同的业务逻辑(一个run方法)需要操作,可实现runnable接口
class BusinessData implements Runnable {
public static Integer count = 100;
@Override
public void run() {
++BusinessData.count;
}
}
设计4个线程,其中两个线程对数据加一,另外两个线程对数据减一:
package com.adam.j2se.thread;
public class MultiThreadShareDataTest {
private static Integer count = 100;
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 2; i++) {
new Thread(new BusinissA()).start();
}
Thread.sleep(2000);
System.out.println(MultiThreadShareDataTest.count);
for (int i = 0; i < 2; i++) {
new Thread(new BusinissB()).start();
}
Thread.sleep(2000);
System.out.println(MultiThreadShareDataTest.count);
}
static class BusinissA implements Runnable {
@Override
public void run() {
MultiThreadShareDataTest.count++;
}
}
static class BusinissB implements Runnable {
@Override
public void run() {
MultiThreadShareDataTest.count--;
}
}
}
使用内部类的方式访问外部类的数据,内部类启动线程去完成相关业务!
五,java线程并发库:
1,线程池: