java代码块
2016-01-01 23:44
465 查看
1.普通代码块(在方法中,按顺序运行,只在块内起作用)
2.构造代码块(在类中,在构造器运行之前运行)
3.静态代码块(在类中,在类加载时运行,且只运行一次)
4.同步代码块(用于线程中)
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
相关文章推荐
- 使用Java zip包和apach 的ant包对文件、文件夹进行压缩,兼容中文
- Java Thread wait, notify和notifyAll示例
- (转)java入门时的一些基本概念的理解(j2ee,j2se,j2me,jdk,sdk,jre,jvm,跨平台)
- [Java EE][Security] - Understanding Security Realms - 1. Main Thought
- (转)Java跨平台原理
- (转)JAVA语言为什么能跨平台?
- [Java]Java开发之路之汇总
- 如何在eclipse中实现参数传递给args[]中
- 求助,javax.persistence.Table.indexes()
- 集合框架基础知识点
- java字符串
- [译]GC专家系列1: 理解Java垃圾回收
- Java学习笔记·Servlet互相传递参数简单实例
- 基于java的任务中转接口
- java15.语言进阶------本地文件操作---遍历文件夹
- JAVA学习笔记(一)——开始
- Java项目打包部署war文件
- Java 和 C 的入门案例
- WebService(2)-XML系列之Java和Xml之间相互转换
- Java语言简介和开发环境搭建