File tree Expand file tree Collapse file tree 3 files changed +13
-14
lines changed
03concurrency/0301/src/main/java/java0/conc0301 Expand file tree Collapse file tree 3 files changed +13
-14
lines changed Original file line number Diff line number Diff line change 22
33public class DaemonThread {
44
5- public static void main (String [] args ) {
6- Runnable task = new Runnable () {
7- @ Override
8- public void run () {
5+ public static void main (String [] args ) throws InterruptedException {
6+ Runnable task = () -> {
97 try {
10- Thread .sleep (5000 );
11- } catch (InterruptedException e ) {
12- e .printStackTrace ();
13- }
8+ Thread .sleep (1000 );
9+ } catch (InterruptedException e ) {
10+ e .printStackTrace ();
11+ }
1412 Thread t = Thread .currentThread ();
1513 System .out .println ("当前线程:" + t .getName ());
16- }
1714 };
1815 Thread thread = new Thread (task );
1916 thread .setName ("test-thread-1" );
20- thread .setDaemon (false );
17+ thread .setDaemon (true );
2118 thread .start ();
19+
20+ //Thread.sleep(2000);
2221 }
2322
2423
Original file line number Diff line number Diff line change @@ -6,10 +6,10 @@ public static void main(String[] args) {
66 Object oo = new Object ();
77
88 MyThread thread1 = new MyThread ("thread1 -- " );
9- thread1 .setOo (oo );
9+ thread1 .setOo (thread1 );
1010 thread1 .start ();
1111
12- synchronized (thread1 ) {
12+ synchronized (thread1 ) { // 这里用oo或thread1/this
1313 for (int i = 0 ; i < 100 ; i ++) {
1414 if (i == 20 ) {
1515 try {
@@ -40,7 +40,7 @@ public MyThread(String name) {
4040
4141 @ Override
4242 public void run () {
43- synchronized (this ) {
43+ synchronized (this ) { // 这里用oo或this,效果不同
4444 for (int i = 0 ; i < 100 ; i ++) {
4545 System .out .println (name + i );
4646 }
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ public class Counter {
77 public static int B =10 ;
88
99 private volatile int sum = 0 ;
10- public synchronized void incr () {
10+ public void incr () {
1111 sum =sum +1 ;
1212 }
1313 public int getSum () {
You can’t perform that action at this time.
0 commit comments