Skip to content

Commit 6db7012

Browse files
committed
新增-慕课网《细说Java多线程之内存可见性》学习源码
1 parent 639988e commit 6db7012

File tree

9 files changed

+389
-0
lines changed

9 files changed

+389
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.myimooc</groupId>
6+
<artifactId>synchronizeddemo</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>synchronizeddemo</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>3.8.1</version>
23+
<scope>test</scope>
24+
</dependency>
25+
</dependencies>
26+
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-compiler-plugin</artifactId>
32+
<configuration>
33+
<source>1.8</source>
34+
<target>1.8</target>
35+
</configuration>
36+
</plugin>
37+
</plugins>
38+
</build>
39+
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.myimooc.synchronizeddemo.my;
2+
3+
/**
4+
* 程序主类
5+
* @author ZhangCheng on 2017-07-09
6+
*
7+
*/
8+
public class SynchronizedDemo {
9+
10+
//共享变量
11+
private boolean ready = false;
12+
private int result = 0;
13+
private int number = 1;
14+
//写操作
15+
public synchronized void write(){
16+
ready = true; //1.1
17+
number = 2; //1.2
18+
}
19+
//读操作
20+
public synchronized void read(){
21+
if(ready){ //2.1
22+
result = number*3; //2.2
23+
}
24+
System.out.println("result的值为:" + result);
25+
}
26+
27+
//内部线程类
28+
private class ReadWriteThread extends Thread {
29+
//根据构造方法中传入的flag参数,确定线程执行读操作还是写操作
30+
private boolean flag;
31+
public ReadWriteThread(boolean flag){
32+
this.flag = flag;
33+
}
34+
@Override
35+
public void run() {
36+
if(flag){
37+
//构造方法中传入true,执行写操作
38+
write();
39+
}else{
40+
//构造方法中传入false,执行读操作
41+
read();
42+
}
43+
}
44+
}
45+
46+
public static void main(String[] args) {
47+
SynchronizedDemo synDemo = new SynchronizedDemo();
48+
//启动线程执行写操作
49+
synDemo.new ReadWriteThread(true).start();
50+
try {
51+
Thread.sleep(1000);
52+
} catch (InterruptedException e) {
53+
e.printStackTrace();
54+
}
55+
//启动线程执行读操作
56+
synDemo.new ReadWriteThread(false).start();
57+
}
58+
59+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.myimooc.synchronizeddemo.teach;
2+
3+
public class SynchronizedDemo {
4+
//共享变量
5+
private boolean ready = false;
6+
private int result = 0;
7+
private int number = 1;
8+
//写操作
9+
public void write(){
10+
ready = true; //1.1
11+
number = 2; //1.2
12+
}
13+
//读操作
14+
public void read(){
15+
if(ready){ //2.1
16+
result = number*3; //2.2
17+
}
18+
System.out.println("result的值为:" + result);
19+
}
20+
21+
//内部线程类
22+
private class ReadWriteThread extends Thread {
23+
//根据构造方法中传入的flag参数,确定线程执行读操作还是写操作
24+
private boolean flag;
25+
public ReadWriteThread(boolean flag){
26+
this.flag = flag;
27+
}
28+
@Override
29+
public void run() {
30+
if(flag){
31+
//构造方法中传入true,执行写操作
32+
write();
33+
}else{
34+
//构造方法中传入false,执行读操作
35+
read();
36+
}
37+
}
38+
}
39+
40+
public static void main(String[] args) {
41+
SynchronizedDemo synDemo = new SynchronizedDemo();
42+
//启动线程执行写操作
43+
synDemo .new ReadWriteThread(true).start();
44+
try {
45+
Thread.sleep(1000);
46+
} catch (InterruptedException e) {
47+
// TODO Auto-generated catch block
48+
e.printStackTrace();
49+
}
50+
//启动线程执行读操作
51+
synDemo.new ReadWriteThread(false).start();
52+
}
53+
}
54+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.myimooc.synchronizeddemo;
2+
3+
import junit.framework.Test;
4+
import junit.framework.TestCase;
5+
import junit.framework.TestSuite;
6+
7+
/**
8+
* Unit test for simple App.
9+
*/
10+
public class AppTest
11+
extends TestCase
12+
{
13+
/**
14+
* Create the test case
15+
*
16+
* @param testName name of the test case
17+
*/
18+
public AppTest( String testName )
19+
{
20+
super( testName );
21+
}
22+
23+
/**
24+
* @return the suite of tests being tested
25+
*/
26+
public static Test suite()
27+
{
28+
return new TestSuite( AppTest.class );
29+
}
30+
31+
/**
32+
* Rigourous Test :-)
33+
*/
34+
public void testApp()
35+
{
36+
assertTrue( true );
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.myimooc</groupId>
6+
<artifactId>volatiledemo</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>volatiledemo</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>3.8.1</version>
23+
<scope>test</scope>
24+
</dependency>
25+
</dependencies>
26+
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-compiler-plugin</artifactId>
32+
<configuration>
33+
<source>1.8</source>
34+
<target>1.8</target>
35+
</configuration>
36+
</plugin>
37+
</plugins>
38+
</build>
39+
</project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.myimooc.volatiledemo.my;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* 程序主类
8+
* @author ZhangCheng on 2017-07-09
9+
*
10+
*/
11+
public class VolatileDemo {
12+
13+
private int number = 0;
14+
private Lock lock = new ReentrantLock();
15+
16+
public int getNumber(){
17+
return this.number;
18+
}
19+
20+
public void increase(){
21+
try {
22+
Thread.sleep(100);
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
}
26+
27+
// 方案一
28+
/*
29+
synchronized(this){
30+
this.number++;
31+
}
32+
*/
33+
// 方案二
34+
lock.lock();// 获取锁
35+
try {
36+
this.number++;
37+
} finally {
38+
lock.unlock();// 释放锁
39+
}
40+
41+
}
42+
43+
public static void main(String[] args) {
44+
final VolatileDemo volDemo = new VolatileDemo();
45+
for(int i = 0 ; i < 500 ; i++){
46+
new Thread(new Runnable() {
47+
@Override
48+
public void run() {
49+
volDemo.increase();
50+
}
51+
}).start();
52+
}
53+
54+
//如果还有子线程在运行,主线程就让出CPU资源,
55+
//直到所有的子线程都运行完了,主线程再继续往下执行
56+
while(Thread.activeCount() > 1){
57+
Thread.yield();
58+
}
59+
60+
System.out.println("number : " + volDemo.getNumber());
61+
}
62+
63+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.myimooc.volatiledemo.teach;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
public class VolatileDemo {
7+
8+
private Lock lock = new ReentrantLock();
9+
private int number = 0;
10+
11+
public int getNumber(){
12+
return this.number;
13+
}
14+
15+
public void increase(){
16+
try {
17+
Thread.sleep(100);
18+
} catch (InterruptedException e) {
19+
// TODO Auto-generated catch block
20+
e.printStackTrace();
21+
}
22+
lock.lock();
23+
try {
24+
this.number++;
25+
} finally {
26+
lock.unlock();
27+
}
28+
}
29+
30+
/**
31+
* @param args
32+
*/
33+
public static void main(String[] args) {
34+
// TODO Auto-generated method stub
35+
final VolatileDemo volDemo = new VolatileDemo();
36+
for(int i = 0 ; i < 500 ; i++){
37+
new Thread(new Runnable() {
38+
39+
@Override
40+
public void run() {
41+
volDemo.increase();
42+
}
43+
}).start();
44+
}
45+
46+
//如果还有子线程在运行,主线程就让出CPU资源,
47+
//直到所有的子线程都运行完了,主线程再继续往下执行
48+
while(Thread.activeCount() > 1){
49+
Thread.yield();
50+
}
51+
52+
System.out.println("number : " + volDemo.getNumber());
53+
}
54+
55+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.myimooc.volatiledemo;
2+
3+
import junit.framework.Test;
4+
import junit.framework.TestCase;
5+
import junit.framework.TestSuite;
6+
7+
/**
8+
* Unit test for simple App.
9+
*/
10+
public class AppTest
11+
extends TestCase
12+
{
13+
/**
14+
* Create the test case
15+
*
16+
* @param testName name of the test case
17+
*/
18+
public AppTest( String testName )
19+
{
20+
super( testName );
21+
}
22+
23+
/**
24+
* @return the suite of tests being tested
25+
*/
26+
public static Test suite()
27+
{
28+
return new TestSuite( AppTest.class );
29+
}
30+
31+
/**
32+
* Rigourous Test :-)
33+
*/
34+
public void testApp()
35+
{
36+
assertTrue( true );
37+
}
38+
}

0 commit comments

Comments
 (0)