专业的JAVA编程教程与资源

网站首页 > java教程 正文

深圳尚学堂:干货来啦!JAVA常用代码(四)

temp10 2025-01-12 18:34:48 java教程 8 ℃ 0 评论

//19.包和包装机制

jar cvf /tmp/test.jar . // 当前目录压缩到test.jar中

深圳尚学堂:干货来啦!JAVA常用代码(四)

jar xvf /tmp/test.jar // 把test.jar解压到当前目录

从指定class运行jar文件

a. Main-Class: HelloWord // 注意中间有一个空格

b. jar cvmf manifest.mf hello.jar HelloWorld.class

c. java -jar hello.jar

*/

// 停止线程 - 不要使用stop()方法

private boolean done = false;

public void run() {

while (!done) {

//todo

}

}

public void shutDown() {

done = true;

}

可以调用shutDown()方法来结束线程

// 如果读取IO的时候出现堵塞,那么可以使用下面方法

public void shutDown() throws IOException {

if (io != null)

io.close();

}

// 启动一线程,等待控制台输入,使用join()方法来暂停当前线程,直到其他线程调用

Thread t = new Thread() {

public void run() {

System.out.println("Reading");

try {

System.in.read();

} catch (IOException e) {

System.err.println(e);

}

System.out.println("Thread finished.");

}

};

System.out.println("Starting");

t.start();

System.out.println("Joining");

try {

t.join();

} catch (InterruptedException e) {

System.out.println("Who dares imterrupt my sleep?");

}

System.out.println("Main finished.");

// 加锁保证同步

Lock lock = new ReentrantLock();

try {

lock.lock();

// todo

} finally {

lock.unlock();

}

线程通信wait(), notify(), notifyAll()

生产者-消费者模式

Executors

//20.Java线程

// 停止线程 - 不要使用stop()方法

private boolean done = false;

public void run() {

while (!done) {

//todo

}

}

public void shutDown() {

done = true;

}

可以调用shutDown()方法来结束线程

// 如果读取IO的时候出现堵塞,那么可以使用下面方法

public void shutDown() throws IOException {

if (io != null)

io.close();

}

// 启动一线程,等待控制台输入,使用join()方法来暂停当前线程,直到其他线程调用

Thread t = new Thread() {

public void run() {

System.out.println("Reading");

try {

System.in.read();

} catch (IOException e) {

System.err.println(e);

}

System.out.println("Thread finished.");

}

};

System.out.println("Starting");

t.start();

System.out.println("Joining");

try {

t.join();

} catch (InterruptedException e) {

System.out.println("Who dares imterrupt my sleep?");

}

System.out.println("Main finished.");

// 加锁保证同步

Lock lock = new ReentrantLock();

try {

lock.lock();

// todo

} finally {

lock.unlock();

}

线程通信wait(), notify(), notifyAll()

生产者-消费者模式

Executors

//21. 内省或“命令类的类”

// 反射

Class c = Class.forName("java.lang.String");

Constructor[] cons = c.getConstructors();

for (int i = 0; i < cons.length; i++) {

System.out.println(cons[i].toString());

}

Method[] meths = c.getMethods();

for (int i = 0; i < meths.length; i++) {

System.out.println(meths[i].toString());

}

// 动态装载类

Class c = Class.forName("java.lang.String");

Object obj = c.newInstance();

// 通过反射调用类的方法

class X {

public void master(String s) {

System.out.println("Working on \"" + s + "\"");

}

}

Class clx = X.class;

Class[] argTypes = {String.class};

Method worker = clx.getMethod("master", argTypes);

Object[] theData = {"Chocolate chips"};

worker.invoke(new X(), theData);

输出: Working on "Chocolate chips"

//22.Java与其他语言的结合

// 执行CMD命令,在Eclipse控制台输出

Process p = Runtime.getRuntime().exec("C:/StudySource/ver.cmd");

p.waitFor(); // 等待命令执行完

BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

String s;

while ((s = br.readLine()) != null)

System.out.println(s);

// 调用Jython - 计算22.0/7

BSFManager manager = new BSFManager();

String[] fntypes = {".py"};

manager.registerScriptingEngine("jython", "org.apache.bsf.engines.jython.JythonEngine", fntypes);

Object r = manager.eval("jython", "testString", 0, 0, "22.0/7");

System.out.println("Result type is " + r.getClass().getName());

System.out.println("Result value is " + r);

// 调用C++,使用JNI

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表