Skip to content

Commit

Permalink
unit2
Browse files Browse the repository at this point in the history
  • Loading branch information
gitpangbo committed Nov 8, 2016
1 parent 9d53415 commit ae7c6d1
Show file tree
Hide file tree
Showing 185 changed files with 4,093 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.kirito.multithreading.unit2.eg_2_1_1;

/***
* Java Lib For MyProject, Powered By Kirito.
* <p>
* -----------------------------------------------------------------------------
* <p>
*
* @Description TODO(用一句话描述该文件做什么)
* @Copyright Copyright (c) 2012-2016
* @version 1.0.0
* @author pangbo
* @createDate 2016年11月7日
* @since JDK1.7
*/
public class HasSelfPrivateNum {

public void addI(String username) {
try {
int num = 0;
if (username.equals("a")) {
num = 100;
System.out.println("a set over");
Thread.sleep(2000);
} else {
num = 200;
System.out.println("b set over");
}
System.out.println(username + " num=" + num);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
27 changes: 27 additions & 0 deletions src/main/java/org/kirito/multithreading/unit2/eg_2_1_1/Run.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.kirito.multithreading.unit2.eg_2_1_1;

/***
* Java Lib For MyProject, Powered By Kirito.
* <p>
* -----------------------------------------------------------------------------
* <p>
*
* @Description TODO(用一句话描述该文件做什么)
* @Copyright Copyright (c) 2012-2016
* @version 1.0.0
* @author pangbo
* @createDate 2016年11月7日
* @since JDK1.7
*/
public class Run {

public static void main(String[] args) {
// TODO Auto-generated method stub
HasSelfPrivateNum numRef = new HasSelfPrivateNum();
ThreadA threadA = new ThreadA(numRef);
threadA.start();
ThreadB threadB = new ThreadB(numRef);
threadB.start();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.kirito.multithreading.unit2.eg_2_1_1;

/***
* Java Lib For MyProject, Powered By Kirito.
* <p>
* -----------------------------------------------------------------------------
* <p>
*
* @Description TODO(用一句话描述该文件做什么)
* @Copyright Copyright (c) 2012-2016
* @version 1.0.0
* @author pangbo
* @createDate 2016年11月7日
* @since JDK1.7
*/
public class ThreadA extends Thread {
private HasSelfPrivateNum numRef;

public ThreadA(HasSelfPrivateNum numRef) {
super();
this.numRef = numRef;
}

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
numRef.addI("a");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.kirito.multithreading.unit2.eg_2_1_1;

/***
* Java Lib For MyProject, Powered By Kirito.
* <p>
* -----------------------------------------------------------------------------
* <p>
*
* @Description TODO(用一句话描述该文件做什么)
* @Copyright Copyright (c) 2012-2016
* @version 1.0.0
* @author pangbo
* @createDate 2016年11月7日
* @since JDK1.7
*/
public class ThreadB extends Thread {
private HasSelfPrivateNum numRef;

public ThreadB(HasSelfPrivateNum numRef) {
super();
this.numRef = numRef;
}

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
numRef.addI("b");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.kirito.multithreading.unit2.eg_2_1_2;

/***
* Java Lib For MyProject, Powered By Kirito.
* <p>
* -----------------------------------------------------------------------------
* <p>
*
* @Description TODO(用一句话描述该文件做什么)
* @Copyright Copyright (c) 2012-2016
* @version 1.0.0
* @author pangbo
* @createDate 2016年11月7日
* @since JDK1.7
*/
public class HasSelfPrivateNum {
private int num = 0;

synchronized public void addI(String username) {
try {
if (username.equals("a")) {
num = 100;
System.out.println("a set over");
Thread.sleep(2000);
} else {
num = 200;
System.out.println("b set over");
}
System.out.println(username + " num=" + num);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
27 changes: 27 additions & 0 deletions src/main/java/org/kirito/multithreading/unit2/eg_2_1_2/Run.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.kirito.multithreading.unit2.eg_2_1_2;

/***
* Java Lib For MyProject, Powered By Kirito.
* <p>
* -----------------------------------------------------------------------------
* <p>
*
* @Description TODO(用一句话描述该文件做什么)
* @Copyright Copyright (c) 2012-2016
* @version 1.0.0
* @author pangbo
* @createDate 2016年11月7日
* @since JDK1.7
*/
public class Run {

public static void main(String[] args) {
// TODO Auto-generated method stub
HasSelfPrivateNum numRef = new HasSelfPrivateNum();
ThreadA threadA = new ThreadA(numRef);
threadA.start();
ThreadB threadB = new ThreadB(numRef);
threadB.start();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.kirito.multithreading.unit2.eg_2_1_2;

/***
* Java Lib For MyProject, Powered By Kirito.
* <p>
* -----------------------------------------------------------------------------
* <p>
*
* @Description TODO(用一句话描述该文件做什么)
* @Copyright Copyright (c) 2012-2016
* @version 1.0.0
* @author pangbo
* @createDate 2016年11月7日
* @since JDK1.7
*/
public class ThreadA extends Thread {
private HasSelfPrivateNum numRef;

public ThreadA(HasSelfPrivateNum numRef) {
super();
this.numRef = numRef;
}

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
numRef.addI("a");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.kirito.multithreading.unit2.eg_2_1_2;

/***
* Java Lib For MyProject, Powered By Kirito.
* <p>
* -----------------------------------------------------------------------------
* <p>
*
* @Description TODO(用一句话描述该文件做什么)
* @Copyright Copyright (c) 2012-2016
* @version 1.0.0
* @author pangbo
* @createDate 2016年11月7日
* @since JDK1.7
*/
public class ThreadB extends Thread {
private HasSelfPrivateNum numRef;

public ThreadB(HasSelfPrivateNum numRef) {
super();
this.numRef = numRef;
}

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
numRef.addI("b");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.kirito.multithreading.unit2.eg_2_1_3;

/***
* Java Lib For MyProject, Powered By Kirito.
* <p>
* -----------------------------------------------------------------------------
* <p>
*
* @Description TODO(用一句话描述该文件做什么)
* @Copyright Copyright (c) 2012-2016
* @version 1.0.0
* @author pangbo
* @createDate 2016年11月7日
* @since JDK1.7
*/
public class HasSelfPrivateNum {
private int num = 0;

synchronized public void addI(String username) {
try {
if (username.equals("a")) {
num = 100;
System.out.println("a set over");
Thread.sleep(2000);
} else {
num = 200;
System.out.println("b set over");
}
System.out.println(username + " num=" + num);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/org/kirito/multithreading/unit2/eg_2_1_3/Run.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.kirito.multithreading.unit2.eg_2_1_3;

/***
* Java Lib For MyProject, Powered By Kirito.
* <p>
* -----------------------------------------------------------------------------
* <p>
*
* @Description TODO(用一句话描述该文件做什么)
* @Copyright Copyright (c) 2012-2016
* @version 1.0.0
* @author pangbo
* @createDate 2016年11月7日
* @since JDK1.7
*/
public class Run {

public static void main(String[] args) {
// TODO Auto-generated method stub
HasSelfPrivateNum numRef1 = new HasSelfPrivateNum();
HasSelfPrivateNum numRef2 = new HasSelfPrivateNum();
ThreadA threadA = new ThreadA(numRef1);
threadA.start();
ThreadB threadB = new ThreadB(numRef2);
threadB.start();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.kirito.multithreading.unit2.eg_2_1_3;

/***
* Java Lib For MyProject, Powered By Kirito.
* <p>
* -----------------------------------------------------------------------------
* <p>
*
* @Description TODO(用一句话描述该文件做什么)
* @Copyright Copyright (c) 2012-2016
* @version 1.0.0
* @author pangbo
* @createDate 2016年11月7日
* @since JDK1.7
*/
public class ThreadA extends Thread {
private HasSelfPrivateNum numRef;

public ThreadA(HasSelfPrivateNum numRef) {
super();
this.numRef = numRef;
}

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
numRef.addI("a");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.kirito.multithreading.unit2.eg_2_1_3;

/***
* Java Lib For MyProject, Powered By Kirito.
* <p>
* -----------------------------------------------------------------------------
* <p>
*
* @Description TODO(用一句话描述该文件做什么)
* @Copyright Copyright (c) 2012-2016
* @version 1.0.0
* @author pangbo
* @createDate 2016年11月7日
* @since JDK1.7
*/
public class ThreadB extends Thread {
private HasSelfPrivateNum numRef;

public ThreadB(HasSelfPrivateNum numRef) {
super();
this.numRef = numRef;
}

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
numRef.addI("b");
}

}
Loading

0 comments on commit ae7c6d1

Please sign in to comment.