java的多线程与并发

cloud

java创建线程

  • extend Thread
  • implements Runnable

java多线程计算器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Caculator implements Runnable{
public void run(){
for(int i = 0; i < 100; i ++){
System.out.println(“%s, value=%d”, Thread.currentThread().getName(), i);
}
}
}

class Application{
public static void main(){
for(int i = 0; i < 10; i ++){
Caculator c = new Caculator();
Thread t = new Thread(c);
t.start();
}
}
}

计数器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Counter implements Runnable{
private int count = 0;
public void run(){
for (int i = 0; i < 10000; i ++){
count ++;
}
}

public static int getCountValue(){
return count;
}
}

public class Application{
public static void main(){
Counter c = new Counter();
Thread t = new Thread();
t.start();
System.out.println(Count.getCountValue());
}
}

使用join方法

  • 子线程调用join方法,让main线程等待
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    public class MySubThread extends Thread{
    public void run(){
    int sleepTime = 2000;
    Thread.sleep(sleepTime);
    System.out.println(“MySubThread run finished!”);
    }
    }

    public class Application{
    public static void main(){
    MySubThread t = new MySubThread();
    t.start();
    // t.join();
    System.out.println(“Main Thread finished!”);
    }
    }

线程上锁

  • synchronized
  • lock与ReentrantedLock

    synchronized版本的计数器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    public class Counter implements Runnable{
    int count = 0;

    public static int getCount(){
    return count;
    }

    @Override
    public void run(){
    for(int i = 0; i < 10000; i ++){
    synchronized(this){
    count ++;
    }
    }
    }
    }

    public class Application{
    public static void main(){
    Counter c = new Counter();
    Thread t1 = new Thread();
    Thread t2 = new Thread();
    t1.start();
    t1.join();
    t2.start();
    t2.join();
    System.out.println(“count is: ” + Count.getCount());
    }
    }

synchronized版本的计数器之二, 使用synchronized给实例方法加锁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Counter implements Runnable{
int count = 0;

public static int getCount(){
return count;
}

@Override
public synchronized void run(){
for(int i = 0; i < 10000; i ++){
count ++;
}
}
}

public class Application{
public static void main(){
Counter c = new Counter();
Thread t1 = new Thread();
Thread t2 = new Thread();
t1.start();
t1.join();
t2.start();
t2.join();
System.out.println(“count is: ” + Count.getCount());
}
}

synchronized版本的计数器之三,使用synchronized修饰静态方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Counter implements Runnable{
int count = 0;

public static int getCount(){
return count;
}

@Override
public void run(){
for(int i = 0; i < 10000; i ++){
increase();
}
}
}

public static synchronize increase(){
count ++;
}
}

public class Application{
public static void main(){
Counter c = new Counter();
Thread t1 = new Thread();
Thread t2 = new Thread();
t1.start();
t1.join();
t2.start();
t2.join();
System.out.println(“count is: ” + Count.getCount());
}
}

笔记

  • reentrant单词也作re-entrant,entrant为进入者、新会员的意思