Skip to content

Commit 24ea319

Browse files
committed
Add Stage 4 Lesson 1
1 parent 94c24c6 commit 24ea319

27 files changed

+1052
-0
lines changed

「一入 Java 深似海 」/代码/segmentfault/deep-in-java/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<module>stage-1</module>
1414
<module>stage-2</module>
1515
<module>stage-3</module>
16+
<module>stage-4</module>
1617
</modules>
1718

1819
<build>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>deep-in-java</artifactId>
7+
<groupId>com.segmentfault</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>stage-4</artifactId>
13+
<packaging>pom</packaging>
14+
<name>「一入 Java 深似海 」系列 :: 第四期</name>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-compiler-plugin</artifactId>
21+
<version>3.8.0</version>
22+
<configuration>
23+
<source>11</source>
24+
<target>11</target>
25+
</configuration>
26+
</plugin>
27+
</plugins>
28+
</build>
29+
<modules>
30+
<module>stage-4-lesson-1</module>
31+
</modules>
32+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>stage-4</artifactId>
7+
<groupId>com.segmentfault</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>stage-4-lesson-1</artifactId>
13+
<name>「一入 Java 深似海 」系列 :: 第四期 :: 第一节</name>
14+
<url></url>
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.segmentfault.deep.in.java.concurrency;
2+
3+
public class HelloWorldThreadDemo {
4+
5+
public static void main(String[] args) throws Exception {
6+
7+
// 创建 Java 线程
8+
// Java 线程对象和 JVM OS 线程并不是同一对象
9+
Thread t1 = new Thread(HelloWorldThreadDemo::helloWorld);
10+
// 主线程 main 显示地启动子线程
11+
t1.start(); // pthread_create()
12+
13+
// 等待线程执行结束
14+
t1.join(); // pthread_join()
15+
16+
// 当线程 isAlive() 返回 false 时,JVM 线程已经消亡了(delete this)
17+
System.out.printf("线程状态 : %s , 是否存活 : %s", t1.getState(), t1.isAlive());
18+
}
19+
20+
static void helloWorld() {
21+
System.out.printf("Thread[id : %d] - Hello World\n",
22+
Thread.currentThread().getId());
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.segmentfault.deep.in.java.concurrency;
2+
3+
import java.util.concurrent.locks.Condition;
4+
import java.util.concurrent.locks.Lock;
5+
import java.util.concurrent.locks.ReentrantLock;
6+
7+
public class SynchronizationDemo {
8+
9+
// pthread_mutex_t lock;
10+
static Lock lock = new ReentrantLock();
11+
12+
static volatile int counter = 0;
13+
14+
public static void main(String[] args) throws Exception {
15+
16+
// pthread_cond_t condition1;
17+
Condition condition1 = lock.newCondition();
18+
19+
// 前提:Lock#lock()
20+
// await() 和 signal() 或 signalAll()
21+
22+
// 前提:synchronized(object) ->
23+
// Object wait() 和 notify() 或 notifyAll();
24+
25+
synchronized (Object.class) {
26+
// Object.class.wait();
27+
}
28+
29+
Thread t1 = new Thread(SynchronizationDemo::addCounter);
30+
Thread t2 = new Thread(SynchronizationDemo::addCounter);
31+
t1.start();
32+
t2.start();
33+
34+
t1.join();
35+
t2.join();
36+
}
37+
38+
private static void addCounter() {
39+
lock.lock(); // pthread_mutex_lock()
40+
// lock.tryLock() // pthread_mutex_trylock()
41+
System.out.println(getThreadPrefix() + "Before Counter : " + counter);
42+
counter++;
43+
System.out.println(getThreadPrefix() + "After Counter : " + counter);
44+
lock.unlock(); // pthread_mutex_unlock()
45+
}
46+
47+
private static String getThreadPrefix() {
48+
return "Thread[" + Thread.currentThread().getId() + "] : ";
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.438
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PosixThread_Attributes", "PosixThread_Attributes\PosixThread_Attributes.vcxproj", "{CFEF4222-68AC-4D9C-98D2-E0E2181277E4}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Debug|x64.ActiveCfg = Debug|x64
17+
{CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Debug|x64.Build.0 = Debug|x64
18+
{CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Debug|x86.ActiveCfg = Debug|Win32
19+
{CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Debug|x86.Build.0 = Debug|Win32
20+
{CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Release|x64.ActiveCfg = Release|x64
21+
{CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Release|x64.Build.0 = Release|x64
22+
{CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Release|x86.ActiveCfg = Release|Win32
23+
{CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {B1D908A1-8B3F-4C04-9913-124FC142A4A3}
30+
EndGlobalSection
31+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// PosixThread_HelloWorld.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
2+
//
3+
4+
#include "pch.h"
5+
#include <iostream>
6+
#include <pthread.h>
7+
8+
using namespace std;
9+
10+
11+
int main()
12+
{
13+
pthread_t t1;
14+
15+
// 申明 POSIX Thread 属性变量
16+
pthread_attr_t attr;
17+
// 申请线程栈大小(无符号整型) 512 K
18+
size_t stack_size = 512 * 1000;
19+
20+
// 初始化 pthread_attr_t
21+
pthread_attr_init(&attr);
22+
// 设置 pthread_attr_t 栈大小 -> 512 K
23+
pthread_attr_setstacksize(&attr, stack_size);
24+
25+
// 创建 t1 线程,并且将执行对象指向 void* helloWorld(void* ptr);
26+
int result = pthread_create(&t1, &attr, helloWorld, NULL);
27+
28+
pthread_join(t1, NULL);
29+
// 销毁 pthread_attr_t attr
30+
pthread_attr_destroy(&attr);
31+
//线程退出
32+
pthread_exit(NULL);
33+
34+
return EXIT_SUCCESS;
35+
}
36+
37+
void* helloWorld(void* ptr) {
38+
pthread_t t = pthread_self();
39+
//printf("Thread - Hello World \n");
40+
cout << "Hello World" << endl;
41+
return NULL;
42+
}
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>15.0</VCProjectVersion>
23+
<ProjectGuid>{CFEF4222-68AC-4D9C-98D2-E0E2181277E4}</ProjectGuid>
24+
<Keyword>Win32Proj</Keyword>
25+
<RootNamespace>PosixThreadAttributes</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>Application</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v141</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>Application</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v141</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>Application</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v141</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>Application</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v141</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
74+
<LinkIncremental>true</LinkIncremental>
75+
<IncludePath>E:\software\dev\CPP\pthreads-w32-2-9-1\Pre-built.2\include;$(IncludePath)</IncludePath>
76+
<LibraryPath>E:\software\dev\CPP\pthreads-w32-2-9-1\Pre-built.2\dll\x86;$(LibraryPath)</LibraryPath>
77+
</PropertyGroup>
78+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
79+
<LinkIncremental>true</LinkIncremental>
80+
</PropertyGroup>
81+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
82+
<LinkIncremental>false</LinkIncremental>
83+
</PropertyGroup>
84+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
85+
<LinkIncremental>false</LinkIncremental>
86+
</PropertyGroup>
87+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
88+
<ClCompile>
89+
<PrecompiledHeader>Use</PrecompiledHeader>
90+
<WarningLevel>Level3</WarningLevel>
91+
<Optimization>Disabled</Optimization>
92+
<SDLCheck>true</SDLCheck>
93+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
94+
<ConformanceMode>true</ConformanceMode>
95+
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
96+
</ClCompile>
97+
<Link>
98+
<SubSystem>Console</SubSystem>
99+
<GenerateDebugInformation>true</GenerateDebugInformation>
100+
<AdditionalDependencies>E:\software\dev\CPP\pthreads-w32-2-9-1\Pre-built.2\lib\x86\pthreadVC2.lib;%(AdditionalDependencies)</AdditionalDependencies>
101+
</Link>
102+
</ItemDefinitionGroup>
103+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
104+
<ClCompile>
105+
<PrecompiledHeader>Use</PrecompiledHeader>
106+
<WarningLevel>Level3</WarningLevel>
107+
<Optimization>Disabled</Optimization>
108+
<SDLCheck>true</SDLCheck>
109+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
110+
<ConformanceMode>true</ConformanceMode>
111+
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
112+
</ClCompile>
113+
<Link>
114+
<SubSystem>Console</SubSystem>
115+
<GenerateDebugInformation>true</GenerateDebugInformation>
116+
</Link>
117+
</ItemDefinitionGroup>
118+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
119+
<ClCompile>
120+
<PrecompiledHeader>Use</PrecompiledHeader>
121+
<WarningLevel>Level3</WarningLevel>
122+
<Optimization>MaxSpeed</Optimization>
123+
<FunctionLevelLinking>true</FunctionLevelLinking>
124+
<IntrinsicFunctions>true</IntrinsicFunctions>
125+
<SDLCheck>true</SDLCheck>
126+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
127+
<ConformanceMode>true</ConformanceMode>
128+
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
129+
</ClCompile>
130+
<Link>
131+
<SubSystem>Console</SubSystem>
132+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
133+
<OptimizeReferences>true</OptimizeReferences>
134+
<GenerateDebugInformation>true</GenerateDebugInformation>
135+
</Link>
136+
</ItemDefinitionGroup>
137+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
138+
<ClCompile>
139+
<PrecompiledHeader>Use</PrecompiledHeader>
140+
<WarningLevel>Level3</WarningLevel>
141+
<Optimization>MaxSpeed</Optimization>
142+
<FunctionLevelLinking>true</FunctionLevelLinking>
143+
<IntrinsicFunctions>true</IntrinsicFunctions>
144+
<SDLCheck>true</SDLCheck>
145+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
146+
<ConformanceMode>true</ConformanceMode>
147+
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
148+
</ClCompile>
149+
<Link>
150+
<SubSystem>Console</SubSystem>
151+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
152+
<OptimizeReferences>true</OptimizeReferences>
153+
<GenerateDebugInformation>true</GenerateDebugInformation>
154+
</Link>
155+
</ItemDefinitionGroup>
156+
<ItemGroup>
157+
<ClInclude Include="pch.h" />
158+
</ItemGroup>
159+
<ItemGroup>
160+
<ClCompile Include="pch.cpp">
161+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
162+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
163+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
164+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
165+
</ClCompile>
166+
<ClCompile Include="PosixThread_Attributes.cpp" />
167+
</ItemGroup>
168+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
169+
<ImportGroup Label="ExtensionTargets">
170+
</ImportGroup>
171+
</Project>

0 commit comments

Comments
 (0)