当前位置:文档之家› 多线程论文

多线程论文

一.线程的创建:
1.
2.
3.
二.T imer传统定时器:
1.
2.
三.线程互斥技术synchronized:
1.线程安全问题可以用银行转账问题解释。

2.
static class Outputer{
public void output(String name){
int len = name.length();
synchronized (Outputer.this){
for(int i=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
public synchronized void output2(String name){
int len = name.length();
for(int i=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
public static synchronized void output3(String name){
int len = name.length();
for(int i=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
四.线程同步问题:
例题:子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着又回到主线程循环100次,如此循环50次,请写出程序。

代码:
new TraditionalThreadCommunication().init();
}
public void init(){
final Business business = new Business();
new Thread(new Runnable() {
@Override
public void run() {
for(int i=1;i<=50;i++){
business.sub(i);
}
}
}).start();
for(int i=1;i<=50;i++){
business.main(i);
}
}
class Business{
private boolean bShouldSub= true;
public synchronized void sub(int i) {
while (!bShouldSub){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(int j=1;j<=10;j++){
System.out.println("sub Thread sequece "+j+",loop of "+i); }
bShouldSub= false;
this.notify();//唤醒主线程
}
public synchronized void main(int i){
while(bShouldSub){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
经验:
1.要用到共同数据(包括同步锁)的若干个方法应该归在同一个类身上,这种设计设
计正好体现了高类聚和程序的健壮性。

2.互斥上锁不是写在线程上的代码,而是要写在线程要访问的资源类内部的!!!
五.线程范围内的共享变量
1. HashMap<Thread,Integer>存取各个线程之间的变量来区分各个线程。

2.
3.ThreadLocal方法:
将上面的HashMap改为:
Private static ThreadLocal<Integer> x = new ThreadLocal<Integer>(); 4.总结:一个ThreadLocal代表一个变量,所以里面只能放一个数据,当有两个变量时,就要定义两个ThreadLocal;但是当变量过大时,就要先将变量封装到类里面,再将这个类放到ThreadLocal中。

5.类中包装:
六.多个线程访问共享对象和数据的方式:
1.如果每一个线程执行的代码相同,则可以使用同一个Runnable对象,这个Runnable
对象中有那个共享数据(例如:卖票)
1.例题:设计4个线程,其中两个线程每次对j加1,两外两个线程每次对j减1,写出
程序:
2.
public class MultiThreadShareData {
private int count=100;
public static void main(String[] args) {
MultiThreadShareData md = new MultiThreadShareData();
Inc ic = md.new Inc();
Dec dc = md.new Dec();
for(int i=0;i<2;i++){
new Thread(ic).start();
new Thread(dc).start();
}
}
private synchronized void inc(){
count++;
System.out.println(Thread.currentThread().getName()+" -inc(): " + count);
}
private synchronized void dec(){
count--;
System.out.println(Thread.currentThread().getName()+" -dec(): " + count);
}
class Inc implements Runnable {
@Override
public void run() {
while (true){
inc();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Dec implements Runnable {
@Override
public void run() {
while (true){
七.AtomicInteger多个线程时,定义变量。

八.线程池(Executors):
1.创建固定大小的线程池
2.创建缓存线程池
3.创建单一线程池(如何实现一个线程死掉后重新启动?)
4代码:
public class ThreadPoolTest {
public static void main(String[] args) {
ExecutorServicethreadpool = Executors.newFixedThreadPool(3); //固定线程池中有三个线程在运行
// ExecutorServicethreadpool =
Executors.newCachedThreadPool(); //动态分配线程池中的线程数
// ExecutorServicethreadpool =
Executors.newSingleThreadExecutor(); //单线程池一个死了立马生成另一个线程
for(int i=1;i<=10;i++){
final int task = i;
threadpool.execute(new Runnable() {
@Override
public void run() {
for(int j=1;j<=10;j++){
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " loop of : "
5.关闭线程池shutdown和shutdownNow
6.用线程池启动定时器:
调用ScheduledExecutorService的schedule方法,返回的ScheduleFuture的对象可以取消任务
支持间隔重复任务的定时方式,不直接支持绝对定时方式,需要转换成相对定时方式。

相关主题