java代码块

1.普通代码块(在方法中,按顺序运行,只在块内起作用)

package com.commoncodeblock;

public class CommonCodeBlock {
    public static void main(String[] args) {
        {
            int x=10;
            System.out.println("x="+x);
        }
        /*only acting in code block*/
        //System.out.println("x="+x);
        int x=100;
        System.out.println("x="+x);
        {
            int y=90;
            System.out.println("y="+y);
        }
    }
}
output:
x=10
x=100
y=90

2.构造代码块(在类中,在构造器运行之前运行)

package com.constructcodeblock;

public class ConstructCodeBlock {
    public static void main(String[] args) {
        Test t=new Test();
        System.out.println(t.getName());
    }

}

class Test {
    private String name;

    public Test() {
        //constructor
        System.out.println("---second,constructor-----");
    }

    public String getName() {
        return this.name;
    }

    {
        /****** run before constructor *******/
        this.name="first";
        System.out.println("----first,construct code block-----");
    }
}
output:
----first,construct code block-----
---second,constructor-----
first

3.静态代码块(在类中,在类加载时运行,且只运行一次)

package com.staticcodeblock;

public class StaticCodeBlock {
    static {
        System.out.println("run while class loading");
    }
    public static void main(String[] args) {
        Test t=new Test();

    }
}

class Test {
    public Test() {
        //constructor
        System.out.println("constructor");
    }

    {
        System.out.println("constructor code block");
    }

    static {
        System.out.println("static code block");
    }
}
output:
run while class loading
static code block
constructor code block
constructor

4.同步代码块(用于线程中)

/*1.*/
package com.synchronizecodeblock;

public class NonSynchronize {
    public static void main(String[] args) {

        new Thread() {
            public void run() {
                for(int i=1;i<5;i++) {
                    System.out.println("同步代码");
                }
            }
        }.start();

        new Thread() {
            public void run() {
                for(int i=1;i<5;i++) {
                    System.out.println("synchronize code");
                }
            }
        }.start();
    }
}
output:
同步代码
同步代码
同步代码
同步代码
synchronize code
synchronize code
synchronize code
synchronize code

/×2.×/
package com.synchronizecodeblock;

public class NonSynchronize1 {
    public static void main(String[] args) {

        new Thread() {
            public void run() {
                for(int i=1;i<5;i++) {
                    System.out.print("这");
                    for(int j=1;j<10000;j++);
                    System.out.print("是");
                    System.out.print("同步");
                    System.out.println("代码");
                }
            }
        }.start();

        new Thread() {
            public void run() {
                for(int i=1;i<5;i++) {
                    System.out.print("this ");
                    System.out.print("is ");
                    System.out.print("synchronize ");
                    System.out.println("code");
                }
            }
        }.start();
    }   
}
output:
这是同步代码
这this is synchronize 是同步代码
这code
this is synchronize code
this is synchronize code
this is synchronize code
是同步代码

/*3.*/
package com.synchronizecodeblock;

public class SynchronizeCodeBlock {
    public static void main(String[] args) {
        /*        创建锁对象       */
        final Object lock=new Object();
        new Thread() {
            public void run() {
                for(int i=1;i<10;i++) {
                    synchronized(lock) {
                        System.out.print("这");
                        for(int j=1;j<100000;j++);
                        System.out.print("是");
                        System.out.print("同步");
                        System.out.println("代码");
                    }
                }
            }
        }.start();

        new Thread() {
            public void run() {
                for(int i=1;i<10;i++) {
                    synchronized(lock) {
                        System.out.print("this ");
                        for(int j=1;j<100000;j++);
                        System.out.print("is ");
                        System.out.print("synchronize ");
                        System.out.println("code");
                    }
                }
            }
        }.start();
    }   
}
output:
这是同步代码
这是同步代码
this is synchronize code
this is synchronize code
this is synchronize code
this is synchronize code
this is synchronize code
this is synchronize code
这是同步代码
这是同步代码
这是同步代码
这是同步代码
这是同步代码
这是同步代码
这是同步代码
this is synchronize code
this is synchronize code
this is synchronize code
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值