File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package Thread ;
2+
3+ //懒汉式,同步操作
4+
5+ public class BankTest {
6+ }
7+
8+ class Bank {
9+ private Bank (){};
10+
11+ private static Bank instance = null ;
12+
13+ public static synchronized Bank getInstance (){
14+ if (instance == null ){
15+ instance = new Bank ();
16+ }
17+ return instance ;
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package Thread ;
2+
3+ //线程的通信
4+
5+ //两个线程,交替打印1-100之间的数
6+
7+ class Number implements Runnable {
8+ private int number = 1 ;
9+
10+ @ Override
11+ public void run () {
12+ while (true ){
13+ synchronized (this ){
14+ if (number <= 100 ){
15+ //唤醒被阻塞的线程
16+ notify ();
17+
18+ try {
19+ Thread .sleep (100 );
20+ } catch (InterruptedException e ) {
21+ e .printStackTrace ();
22+ }
23+
24+ System .out .println (Thread .currentThread ().getName () + ":" + number );
25+ number ++;
26+
27+ //阻塞当前的线程,并释放锁
28+ try {
29+ wait ();
30+ } catch (InterruptedException e ) {
31+ e .printStackTrace ();
32+ }
33+
34+ }else {
35+ break ;
36+ }
37+ }
38+
39+ }
40+ }
41+ }
42+
43+ public class CommunicationTest {
44+ public static void main (String [] args ) {
45+ Number num = new Number ();
46+
47+ Thread t1 = new Thread (num );
48+ Thread t2 = new Thread (num );
49+
50+ t1 .setName ("线程1" );
51+ t2 .setName ("线程2" );
52+
53+ t1 .start ();
54+ t2 .start ();
55+ }
56+ }
Original file line number Diff line number Diff line change 1+ package Thread ;
2+
3+
4+ //演示线程的死锁
5+ public class LockTest {
6+ public static void main (String [] args ) {
7+ StringBuffer s1 = new StringBuffer ();
8+ StringBuffer s2 = new StringBuffer ();
9+
10+
11+
12+ new Thread (){
13+ @ Override
14+ public void run () {
15+
16+ synchronized (s1 ){
17+ s1 .append ("a" );
18+ s2 .append ("1" );
19+
20+ try {
21+ Thread .sleep (100 );
22+ } catch (InterruptedException e ) {
23+ e .printStackTrace ();
24+ }
25+
26+ synchronized (s2 ){
27+ s1 .append ("b" );
28+ s2 .append ("2" );
29+
30+ System .out .println (s1 );
31+ System .out .println (s2 );
32+ }
33+ }
34+ }
35+
36+ }.start ();
37+
38+ new Thread (new Runnable () {
39+ @ Override
40+ public void run () {
41+ synchronized (s2 ){
42+ s1 .append ("c" );
43+ s2 .append ("3" );
44+
45+ try {
46+ Thread .sleep (100 );
47+ } catch (InterruptedException e ) {
48+ e .printStackTrace ();
49+ }
50+
51+ synchronized (s1 ){
52+ s1 .append ("d" );
53+ s2 .append ("4" );
54+
55+ System .out .println (s1 );
56+ System .out .println (s2 );
57+ }
58+ }
59+ }
60+ }).start ();
61+ }
62+ }
Original file line number Diff line number Diff line change 1+ package Thread ;
2+
3+ //解决线程安全问题方式三:Lock
4+
5+ import java .util .concurrent .locks .ReentrantLock ;
6+
7+ class WindowLock implements Runnable {
8+ private int ticket = 100 ;
9+
10+ //1.实例化ReentrantLock
11+ private ReentrantLock lock = new ReentrantLock ();
12+ @ Override
13+ public void run () {
14+
15+
16+ while (true ){
17+ try {
18+ //2.调用锁定方法lock()
19+ lock .lock ();
20+
21+ if (ticket > 0 ){
22+
23+ try {
24+ Thread .sleep (100 );
25+ }catch (InterruptedException e ){
26+ e .printStackTrace ();
27+ }
28+
29+ System .out .println (Thread .currentThread ().getName () + ":卖票,票号为:" + ticket );
30+ ticket --;
31+ }else {
32+ break ;
33+ }
34+ }finally {
35+ //3.调用解锁方法unlock()
36+ lock .unlock ();
37+ }
38+
39+ }
40+ }
41+ }
42+
43+ public class LockTest1 {
44+ public static void main (String [] args ) {
45+ WindowLock wl = new WindowLock ();
46+
47+ Thread t1 =new Thread (wl );
48+ Thread t2 =new Thread (wl );
49+ Thread t3 =new Thread (wl );
50+
51+ t1 .setName ("窗口1" );
52+ t2 .setName ("窗口2" );
53+ t3 .setName ("窗口3" );
54+
55+ t1 .start ();
56+ t2 .start ();
57+ t3 .start ();
58+ }
59+ }
Original file line number Diff line number Diff line change 1+ package Thread ;
2+
3+ //创建两个分线程,其中一个线程遍历1-100的偶数,另一个线程遍历1-100的基数
4+
5+ public class ThreadDemo {
6+ public static void main (String [] args ) {
7+ MyThread1 m1 = new MyThread1 ();
8+ MyThread2 m2 = new MyThread2 ();
9+ m1 .start ();
10+ m2 .start ();
11+
12+ // 也可以使用匿名对象的方式执行
13+ }
14+ }
15+
16+ class MyThread1 extends Thread {
17+ public void run (){
18+ for (int i = 0 ; i < 100 ; i ++) {
19+ if ( i % 2 == 0 ){
20+ System .out .println (Thread .currentThread ().getName () + ":" + i );
21+ }
22+ }
23+ }
24+ }
25+
26+ class MyThread2 extends Thread {
27+ public void run (){
28+ for (int i = 0 ; i < 100 ; i ++) {
29+ if ( i % 2 !=0 ){
30+ System .out .println (Thread .currentThread ().getName () + ":" + i );
31+ }
32+ }
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ package Thread ;
2+
3+ /*
4+ 测试Thread中常见的方法
5+ 1.start():启动当前线程,调用当前线程的run()
6+ 2.run():通常需要重写该方法以实现某功能
7+ 3.CurrentThread():静态方法,返回当前代码执行的线程
8+ 4.getName():获取当前线程的名字
9+ 5.setName():设置当前线程的名字
10+ 6.yield():释放当前cpu的执行权
11+ 7.join():在线程a中调用线程b的join()方法,此时线程a就进入阻塞状态,直到线程b执行完毕,线程a才结束阻塞状态
12+ 8.stop():已过时,强制结束当前线程
13+ 9.sleep(long millis):静态方法,让当前线程处于"睡眠"状态,在指定的millitime时间内,线程处于阻塞状态
14+ 10.isAlive():判断当前线程是否存活
15+
16+
17+ 线程的优先级:
18+ MAX_PRIORITY:10
19+ MIN_PRIORITY:1
20+ NORM_PRIORITY:5
21+
22+ getPriority():获取
23+ setPriority(int p):设置
24+
25+ 说明:高优先级并不意味着一定先执行,只是从概率上讲,更可能优先被执行
26+
27+ */
28+
29+ class HelloThread extends Thread {
30+ @ Override
31+ public void run () {
32+ for (int i = 0 ; i < 100 ; i ++) {
33+ if (i % 2 == 0 ) {
34+
35+ try {
36+ sleep (10 );
37+ } catch (InterruptedException e ) {
38+ e .printStackTrace ();
39+ }
40+
41+ System .out .println (Thread .currentThread ().getName () + ":" + i );
42+ }
43+ // if(i % 20 == 0){
44+ // yield();
45+ // }
46+ }
47+ }
48+ }
49+
50+ public class ThreadMethodTest {
51+ public static void main (String [] args ) {
52+ HelloThread h1 = new HelloThread ();
53+
54+ h1 .setName ("线程一" );
55+ h1 .start ();
56+
57+ //给主线程命名
58+ Thread .currentThread ().setName ("主线程" );
59+ for (int i = 0 ; i < 100 ; i ++) {
60+ if (i % 2 == 0 ) {
61+ System .out .println (Thread .currentThread ().getName () + ":" + i );
62+ }
63+
64+ if (i == 20 ){
65+ try {
66+ h1 .join ();
67+ } catch (InterruptedException e ) {
68+ e .printStackTrace ();
69+ }
70+ }
71+ }
72+
73+ System .out .println (h1 .isAlive ());
74+ }
75+ }
Original file line number Diff line number Diff line change 1+ package Thread ;
2+
3+ //JDK5.0新增的 实现Callable接口
4+
5+ import java .util .concurrent .Callable ;
6+ import java .util .concurrent .ExecutionException ;
7+ import java .util .concurrent .FutureTask ;
8+
9+ //1实现Callable接口的实现类
10+ class NumThread implements Callable {
11+ int sum = 0 ;
12+ //2实现call(),此方法有返回值
13+ @ Override
14+ public Object call () throws Exception {
15+ for (int i = 1 ; i <= 100 ; i ++) {
16+ sum += i ;
17+ }
18+ return sum ;
19+ }
20+ }
21+
22+
23+ public class ThreadNew {
24+ public static void main (String [] args ) {
25+ //3创建Callable接口实现类的对象
26+ NumThread numThread = new NumThread ();
27+ //4将此对象作为参数传递到FutureTask构造器中,创建FutureTask对象
28+ FutureTask futureTask = new FutureTask (numThread );
29+ //将FutureTask对象传递到Thread类的构造器中,创建Thread对象,并执行start()方法
30+ new Thread (futureTask ).start ();
31+
32+ try {
33+ //get()为FutureTask构造器参数Callable实现类重写的call()的返回值
34+ Object sum = futureTask .get ();
35+ System .out .println (sum );
36+ } catch (InterruptedException e ) {
37+ e .printStackTrace ();
38+ } catch (ExecutionException e ) {
39+ e .printStackTrace ();
40+ }
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ package Thread ;
2+
3+ //创建线程的方式四:线程池
4+
5+ import java .util .concurrent .ExecutorService ;
6+ import java .util .concurrent .Executors ;
7+
8+
9+ class NumberThread implements Runnable {
10+ @ Override
11+ public void run () {
12+ for (int i = 1 ; i <= 100 ; i ++) {
13+ if (i % 2 == 0 ){
14+ System .out .println (i );
15+ }
16+ }
17+ }
18+ }
19+
20+ public class ThreadPool {
21+ public static void main (String [] args ) {
22+ //1提供指定数量的线程池
23+ ExecutorService service = Executors .newFixedThreadPool (10 );
24+ //2执行指定的线程操作
25+ service .execute (new NumberThread ()); // 适合Runnable接口
26+ //service.submit(); //适合Callable接口
27+ //3需要手动关闭连接池
28+ service .shutdown ();
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments