注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

和申的个人主页

专注于java开发,1985wanggang

 
 
 

日志

 
 

Groovy 练习笔记  

2012-03-27 22:34:13|  分类: groovy |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
groovy数组,调用方法练习

1.Dog.groovy文件

package com.heshen.groovy

class Dog {
def bark() {
Thread.sleep(100);
println 'woof!'
}

def sit() {
//Thread.sleep(100);
println 'sitting!'
ss1()
}

def ss1(){
println 'ss1'
ss2()
}

def ss2(){
println 'ss2'
}

def jump() {
Thread.sleep(100);
println 'boing!'
}
}


调用

/**
*
*/
package com.heshen.groovy

/**
* @author wanggang
*
*/
class Test2 {

static main(args) {
def dog = new Dog();
def acts = ['what\'s is the 1':'sit','what\'s is the 2':'jump','what\'s is the 3':'bark']
acts.each {
println acts["${it.key}"]
def method = acts["${it.key}"];
dog."${method}"();
}
long start = System.currentTimeMillis();

for(int i=0;i<100000;i++){
def method1 = acts["what\'s is the 1"];
dog."${method1}"();
}
System.out.println("timeout:" +(System.currentTimeMillis() - start));
start = System.currentTimeMillis();
def method1 = acts["what\'s is the 1"];
dog."${method1}"();
System.out.println("timeout:" +(System.currentTimeMillis() - start));
}

}


调用时使用java的线程池:

package com.heshen.groovy

import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

/**
* @author wanggang
*
*/
class Test {

static main(args) {


ExecutorService executors = Executors.newFixedThreadPool(100);


for(int i = 0;i<1;i++)
executors.submit(new Runnable(){
public void run(){

def dog = new Dog();
def acts = ['what\'s is the 1':'sit','what\'s is the 2':'jump','what\'s is the 3':'bark']
acts.each {
println acts["${it.key}"]
def method = acts["${it.key}"];
dog."${method}"();
}
long start = System.currentTimeMillis();
def method1 = acts["what\'s is the 1"];
System.out.println("timeout:" +(System.currentTimeMillis() - start));
dog."${method1}"();
System.out.println("timeout:" +(System.currentTimeMillis() - start));
}
});

executors.shutdown();

}

}


使用java的方法反射调用:

/**
*
*/
package com.heshen.groovy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* @author wanggang
*
*/
public class T {

/**
* @param args
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Dog dog =new Dog();

Class clazz = dog.getClass();
Method m = clazz.getMethod("sit");
for(int i=0;i<10;i++)
m.invoke(dog);

long start = System.currentTimeMillis();
m.invoke(dog);
System.out.println("timeout:" +(System.currentTimeMillis() - start));
}

}




package com.heshen.test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.util.LinkedList;

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import groovy.lang.Script;

public class Test3 {
public static void main(String[] args) throws IOException {
ClassLoader parent = Test3.class.getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
LinkedList<String> scriptList = null;
URL url =Test3.class.getClassLoader().getResource("com/heshen/groovy/Dog.txt");
System.out.println(url);
String script = readFile(url.getPath());
try {
scriptList = parseScript(script);
} catch (Exception e1) {
new Exception("Parse groovy code error", e1);
}
Class<?> groovyClass = null;
for (String str : scriptList) {
groovyClass = loader.parseClass(str);
}
GroovyObject groovyObject = null;
try {
groovyObject = (GroovyObject) groovyClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
long start = 0;
for(int i=0;i< 12;i++){
if(i>50000){
start = System.currentTimeMillis();
}
try {
groovyObject.getMetaClass().invokeMethod(groovyObject, "sit", new Object[] { });
} catch (Exception e) {
e.printStackTrace();
}
if(i>50000){
System.out.println("timeout:" +(System.currentTimeMillis() - start));
}
}

start = System.currentTimeMillis();
try {
groovyObject.getMetaClass().invokeMethod(groovyObject, "sit", new Object[] { });
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("timeout final:" +(System.currentTimeMillis() - start));


}
private static String readFile(String fileName) throws IOException{
System.out.println(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine()) != null){
sb.append(line);
sb.append("\n");
}
return sb.toString();

}

private static LinkedList<String> parseScript(String script) throws IOException {

LinkedList<String> stack = new LinkedList<String>();
String line = null;
StringBuilder s = new StringBuilder();
StringReader stringReader = new StringReader(script);
BufferedReader reader = new BufferedReader(stringReader);
while ((line = reader.readLine()) != null) {
if ("/**************************************/".equals(line)) {
if (s.length() != 0) {
String part = new String(s.toString());
stack.addLast(part);
s.delete(0, s.length());
}
} else {
s.append(line).append("\n");
}
}
if (s.length() != 0) {
String lastpart = new String(s.toString());
stack.addLast(lastpart);
}
reader.close();
return stack;
}
}



  评论这张
 
阅读(1002)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016