synchronized在方法上加鎖
- 加在非靜態方法上。鎖住的是this對象
class Test{
public synchronized void test(){}
}
等價於
class Test{
public void test(){
synchronized(this){
}
}
}
- 加在靜態方法上。鎖住的是類對象。
class Test{
public synchronized static void test(){
}
}
等價於
class Test{
public static void test(){
synchronized(Test.class){
}
}
}
線程八鎖
爲了體現出這個 1s的輸出,可以在Number類方法a裏面,debug輸出一些值。
1.
輸出順序:12 或21或者同時輸出
public class Demo{
private static Logger logger = Logger.getLogger(Demo.class);
public static void main(String[] agrs) throws InterruptedException {
Number n1=new Number();
new Thread(()->{n1.a();},"t1").start();
new Thread(()->{n1.b();},"t2").start();
}
}
class Number{
private static Logger logger = Logger.getLogger(Number.class);
public synchronized void a(){
logger.debug("1");
}
public synchronized void b(){
logger.debug("2");
}
}
2.
輸出順序的可能情況:
- 等待1秒,輸出12
- 輸出2,等1秒,輸出1
public class Demo{
private static Logger logger = Logger.getLogger(Demo.class);
public static void main(String[] agrs) throws InterruptedException {
Number n1=new Number();
new Thread(()->{
try {
n1.a();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->{n1.b();},"t2").start();
}
}
class Number{
private static Logger logger = Logger.getLogger(Number.class);
public synchronized void a() throws InterruptedException {
Thread.sleep(1000);
logger.debug("1");
}
public synchronized void b(){
logger.debug("2");
}
}
3.
3 等一秒,12
3 2 等一秒,1
2 3 等一秒,1
import org.apache.log4j.Logger;
public class Demo{
private static Logger logger = Logger.getLogger(Demo.class);
public static void main(String[] agrs) throws InterruptedException {
Number n1=new Number();
new Thread(()->{
try {
n1.a();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->{n1.b();},"t2").start();
new Thread(()->{n1.c();},"t3").start();
}
}
class Number{
private static Logger logger = Logger.getLogger(Number.class);
public synchronized void a() throws InterruptedException {
Thread.sleep(1000);
logger.debug("1");
}
public synchronized void b(){
logger.debug("2");
}
public void c(){
logger.debug("3");
}
}
4.
2 等一秒 1
解釋:鎖住的不同的對象。
t1和t2並行。由於t1睡眠的時候,t2還在運行,所以先輸出2
public class Demo{
private static Logger logger = Logger.getLogger(Demo.class);
public static void main(String[] agrs) throws InterruptedException {
Number n1=new Number();
Number n2=new Number();
new Thread(()->{
try {
n1.a();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->{n2.b();},"t2").start();
}
}
class Number{
private static Logger logger = Logger.getLogger(Number.class);
public synchronized void a() throws InterruptedException {
Thread.sleep(1000);
logger.debug("1");
}
public synchronized void b(){
logger.debug("2");
}
}
5.
鎖住不同的對象。
2 等一秒 1
public class Demo{
private static Logger logger = Logger.getLogger(Demo.class);
public static void main(String[] agrs) throws InterruptedException {
Number n1=new Number();
Number n2=new Number();
new Thread(()->{
try {
n1.a();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();//鎖住Number類對象
new Thread(()->{n2.b();},"t2").start();//鎖住n1對象
}
}
class Number{
private static Logger logger = Logger.getLogger(Number.class);
public synchronized static void a() throws InterruptedException {
Thread.sleep(1000);
logger.debug("1");
}
public synchronized void b(){
logger.debug("2");
}
}
6.
鎖住的同一個對象,存在互斥。
1s 12 或者 2 1s 1
public class Demo{
private static Logger logger = Logger.getLogger(Demo.class);
public static void main(String[] agrs) throws InterruptedException {
Number n1=new Number();
new Thread(()->{
try {
n1.a();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->{n1.b();},"t2").start();
}
}
class Number{
private static Logger logger = Logger.getLogger(Number.class);
public synchronized static void a() throws InterruptedException {
Thread.sleep(1000);
logger.debug("1");
}
public synchronized static void b(){
logger.debug("2");
}
}
7.
不存在互斥,2 1s 1
import org.apache.log4j.Logger;
public class Demo{
private static Logger logger = Logger.getLogger(Demo.class);
public static void main(String[] agrs) throws InterruptedException {
Number n1=new Number();
Number n2=new Number();
new Thread(()->{
try {
n1.a();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->{n2.b();},"t2").start();
}
}
class Number{
private static Logger logger = Logger.getLogger(Number.class);
public synchronized static void a() throws InterruptedException {
Thread.sleep(1000);
logger.debug("1");
}
public synchronized void b(){
logger.debug("2");
}
}
8.
鎖住的同一個對象,存在互斥。
1s 12 或者 2 1s 1
public class Demo{
private static Logger logger = Logger.getLogger(Demo.class);
public static void main(String[] agrs) throws InterruptedException {
Number n1=new Number();
Number n2=new Number();
new Thread(()->{
try {
n1.a();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->{n2.b();},"t2").start();
}
}
class Number{
private static Logger logger = Logger.getLogger(Number.class);
public synchronized static void a() throws InterruptedException {
Thread.sleep(1000);
logger.debug("1");
}
public synchronized static void b(){
logger.debug("2");
}
}