「判断java中使用run」判断语句java
今天给各位分享判断java中使用run的知识,其中也会对判断语句java进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
JAVA中run()问题
调用的是同一个run方法,但是在Thread中,会新建一个线程执行这段代码,不会影响当前线程的工作。否则run执行起来,当前的进程就阻塞勒。
java中Runtime类的用法
1、内存管理: Java提供了无用单元自动收集机制。通过totalMemory()和freeMemory()方法可以知道对象的堆内存有多大,还剩多少。 Java会周期性的回收垃圾对象(未使用的对象),以便释放内存空间。但是如果想先于收集器的下一次指定周期来收集废弃的对象,可以通过调用gc()方法来根据需要运行无用单元收集器。一个很好的试验方法是先调用gc()方法,然后调用freeMemory()方法来查看基本的内存使用情况,接着执行代码,然后再次调用freeMemory()方法看看分配了多少内存。下面的程序演示了这个构想。
class MemoryDemo{
public static void main(String args[]){
Runtime r = Runtime.getRuntime();
long mem1,mem2;
Integer someints[] = new Integer[1000];
System.out.println("Total memory is :" + r.totalMemory());
mem1 = r.freeMemory();
System.out.println("Initial free is : " + mem1);
r.gc();
mem1 = r.freeMemory();
System.out.println("Free memory after garbage collection : " + mem1);
//allocate integers
for(int i=0; i1000; i++) someints = new Integer(i);
mem2 = r.freeMemory();
System.out.println("Free memory after allocation : " + mem2);
System.out.println("Memory used by allocation : " +(mem1-mem2));
//discard Intergers
for(int i=0; i1000; i++) someints = null;
r.gc();
//request garbage collection
mem2 = r.freeMemory();
System.out.println("Free memory after collecting " + "discarded integers : " + mem2);
}
}
编译后运行结果如下(不同的机器不同时间运行的结果也不一定一样):
Total memory is :2031616 Initial free is : 1818488 Free memory after garbage collection : 1888808 Free memory after allocation : 1872224 Memory used by allocation : 16584 Free memory after collecting discarded integers : 1888808
2、执行其他程序 在安全的环境中,可以在多任务操作系统中使用Java去执行其他特别大的进程(也就是程序)。exec()方法有几种形式命名想要运行的程序和它的输入参数。exec()方法返回一个Process对象,可以使用这个对象控制Java程序与新运行的进程进行交互。exec()方法本质是依赖于环境。 下面的例子是使用exec()方法启动windows的记事本notepad。这个例子必须在Windows操作系统上运行。
class ExecDemo {
public static void main(String args[]){
Runtime r = Runtime.getRuntime();
Process p = null; try{ p = r.exec("notepad");
} catch (Exception e) {
System.out.println("Error executing notepad.");
}
}
}
exec()还有其他几种形式,例子中演示的是最常用的一种。exec()方法返回Process对象后,在新程序开始运行后就可以使用Process的方法了。可以用destory()方法杀死子进程,也可以使用waitFor()方法等待程序直到子程序结束,exitValue()方法返回子进程结束时返回的值。如果没有错误,将返回0,否则返回非0。下面是关于exec()方法的例子的改进版本。例子被修改为等待,直到运行的进程退出:
class ExecDemoFini {
public static void main(String args[]){
Runtime r = Runtime.getRuntime();
Process p = null;
try{ p = r.exec("notepad");
p.waitFor();
} catch (Exception e) {
System.out.println("Error executing notepad.");
}
System.out.println("Notepad returned " + p.exitValue());
} }
下面是运行的结果(当关闭记事本后,会接着运行程序,打印信息):
Notepad returned 0
按任意键继续. . .
当子进程正在运行时,可以对标准输入输出进行读写。
getOutputStream()方法和getInPutStream()方法返回对子进程的标准输入和输出。
API预览
addShutdownHook(Thread hook) 注册新的虚拟机来关闭挂钩。
availableProcessors() 向 Java 虚拟机返回可用处理器的数目。
exec(String command) 在单独的进程中执行指定的字符串命令。
exec(String[] cmdarray) 在单独的进程中执行指定命令和变量。
exec(String[] cmdarray, String[] envp) 在指定环境的独立进程中执行指定命令和变量。
exec(String[] cmdarray, String[] envp, File dir) 在指定环境和工作目录的独立进程中执行指定的命令和变量。
exec(String command, String[] envp) 在指定环境的单独进程中执行指定的字符串命令。
exec(String command, String[] envp, File dir) 在有指定环境和工作目录的独立进程中执行指定的字符串命令。
exit(int status) 通过启动虚拟机的关闭序列,终止当前正在运行的 Java 虚拟机。 freeMemory() 返回 Java 虚拟机中的空闲内存量。
gc() 运行垃圾回收器。
InputStream getLocalizedInputStream(InputStream in) 已过时。 从 JDK 1.1 开始,将本地编码字节流转换为 Unicode 字符流的首选方法是使用 InputStreamReader 和 BufferedReader 类。
OutputStream getLocalizedOutputStream(OutputStream out) 已过时。 从 JDK 1.1 开始,将 Unicode 字符流转换为本地编码字节流的首选方法是使用 OutputStreamWriter、BufferedWriter 和 PrintWriter 类。
getRuntime() 返回与当前 Java 应用程序相关的运行时对象。
halt(int status) 强行终止目前正在运行的 Java 虚拟机。
load(String filename) 加载作为动态库的指定文件名。
loadLibrary(String libname) 加载具有指定库名的动态库。
maxMemory() 返回 Java 虚拟机试图使用的最大内存量。
removeShutdownHook(Thread hook) 取消注册某个先前已注册的虚拟机关闭挂钩。
runFinalization() 运行挂起 finalization 的所有对象的终止方法。
runFinalizersOnExit(value) 已过时。 此方法本身具有不安全性。它可能对正在使用的对象调用终结方法,而其他线程正在操作这些对象,从而导致不正确的行为或死锁。
totalMemory() 返回 Java 虚拟机中的内存总量。
traceInstructions(on) 启用/禁用指令跟踪。
traceMethodCalls(on) 启用/禁用方法调用跟踪。
java中thread的start和run的区别
java中thread的start()和run()的区别:
1.start()方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码:
通过调用Thread类的start()方法来启动一个线程,
这时此线程是处于就绪状态,
并没有运行。
然后通过此Thread类调用方法run()来完成其运行操作的,
这里方法run()称为线程体,
它包含了要执行的这个线程的内容,
Run方法运行结束,
此线程终止,
而CPU再运行其它线程,
2.run()方法当作普通方法的方式调用,程序还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码:
而如果直接用Run方法,
这只是调用一个方法而已,
程序中依然只有主线程--这一个线程,
其程序执行路径还是只有一条,
这样就没有达到写线程的目的。
举例说明一下:
记住:线程就是为了更好地利用CPU,
提高程序运行速率的!
public class TestThread1{
public static void main(String[] args){
Runner1 r=new Runner1();
//r.run();//这是方法调用,而不是开启一个线程
Thread t=new Thread(r);//调用了Thread(Runnable target)方法。且父类对象变量指向子类对象。
t.start();
for(int i=0;i100;i++){
System.out.println("进入Main Thread运行状态");
System.out.println(i);
}
}
}
class Runner1 implements Runnable{ //实现了这个接口,jdk就知道这个类是一个线程
public void run(){
for(int i=0;i100;i++){
System.out.println("进入Runner1运行状态");
System.out.println(i);
}
}
}
同时摘取一段外文网站论坛上的解释:
Why do we need start() method in Thread class? In Java API description for Thread class is written : "Java Virtual Machine calls the run method of this thread..".
Couldn't we call method run() ourselves, without doing double call: first we call start() method which calls run() method? What is a meaning to do things such complicate?
There is some very small but important difference between using start() and run() methods. Look at two examples below:
Example one:
Code:
Thread one = new Thread();
Thread two = new Thread();
one.run();
two.run();
Example two:
Code:
Thread one = new Thread();
Thread two = new Thread();
one.start();
two.start();
The result of running examples will be different.
In Example one the threads will run sequentially: first, thread number one runs, when it exits the thread number two starts.
In Example two both threads start and run simultaneously.
Conclusion: the start() method call run() method asynchronously (does not wait for any result, just fire up an action), while we run run() method synchronously - we wait when it quits and only then we can run the next line of our code.
Thread对象的run()方法在一种循环下,使线程一直运行,直到不满足条件为止,在你的main()里创建并运行了一些线程,调用Thread类的start()方法将为线程执行特殊的初始化的过程,来配置线程,然后由线程执行机制调用run()。如果你不调用start()线程就不会启动。
因为线程调度机制的行为是不确定的,所以每次运行该程序都会有不同的结果,你可以把你的循环次数增多些,然后看看执行的结果,你会发现main()的线程和Thread1是交替运行的。
4.还有就是尽管线程的调度顺序是不固定的,但是如果有很多线程被阻塞等待运行,调度程序将会让优先级高的线程先执行,而优先级低的线程执行的频率会低一些。
判断java中使用run的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于判断语句java、判断java中使用run的信息别忘了在本站进行查找喔。
发布于:2022-12-01,除非注明,否则均为
原创文章,转载请注明出处。