「java销毁方法」java线程销毁方法

博主:adminadmin 2022-12-13 20:39:12 67

今天给各位分享java销毁方法的知识,其中也会对java线程销毁方法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

销毁对象在java中怎么用

java中的对象是由java自动销毁的。当对象不存在引用时,它就会被java自动销毁。

System.gc()可以提醒java去回收无用的对象,但是什么时候销毁该对象还是由java自己决定

JAVA怎样去销毁一个标签

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.Timer;

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

/**

*

* @author daniel

*/

public class Demo extends JFrame {

private int count = 0;

Timer timer;

public Demo() {

setLayout(null);

addMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent e) {

timer = new Timer(1000, new TimerListener());

timer.start();

}

public void mouseReleased(MouseEvent e) {

timer.stop();

{

getContentPane().removeAll();

System.gc();

}

getContentPane().repaint();

}

});

// 创建窗体

this.setTitle("按下鼠标不动信息交替出现..松开消失");

this.setSize(400, 400);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

class TimerListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

{

getContentPane().removeAll();

System.gc();

}

getContentPane().repaint();

if ((count % 2) == 0) {

JLabel lal_1 = new JLabel("JAVA is fun");

lal_1.setSize(100, 100);

getContentPane().add(lal_1);

lal_1.setLocation(100, 100);

lal_1.setVisible(true);

} else {

JLabel lal_2 = new JLabel("JAVA is powerful");

lal_2.setSize(100, 100);

getContentPane().add(lal_2);

lal_2.setLocation(250, 100);

lal_2.setVisible(true);

}

count += 1;

}

}

public static void main(String[] args) {

Demo d = new Demo();

}

}

java的特点是“永远不需销毁对象”,也似乎没有手动销毁对象的机制。它有一个垃圾回收站,当一个对象出了作用域,且引用数为0时,就可能被垃圾回收。System.gc()用于强制进行终结动作,可加速清理。

为了能够及时清理某个对象,就将它的作用域设置得尽量小。将对象作为类成员的话,作用域太大,要等类销毁时才有机会被回收。

如果觉得getContentPane().removeAll()会同时remove掉其它对象的话,将lal_1和lal_2用一个小一点的容器装起来。

java中Session针对单独对象的销毁

removeAttribute(String name),删除指定名字的session属性,若该属性不存在,则出现异常。

session对象的销毁的方法:手动销毁、配置文件设置时间销毁。

1 当需要在程序中手动设置Session失效时,可以手工调用方法,摧毁session。

removeAttribute(String name),删除指定名字的session属性,若该属性不存在,则出现异常。

public void invalidate(),使session失效。可以立即使当前会话失效,原来会话中存储的所有对象都不能再被访问。

2 session对象默认30分钟没有使用,则服务器会自动销毁session,在web.xml文件中可以手工配置session的失效时间。

Java怎样销毁控制和窗口

方式一

import java.io.IOException;

public class Test {

public static void main(String[] args){

//执行批处理文件

String strcmd="cmd /c start D:\\antrelease.bat";

Runtime rt = Runtime.getRuntime();

Process ps = null;

try {

ps = rt.exec(strcmd);

} catch (IOException e1) {

e1.printStackTrace();

}

try {

ps.waitFor();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

int i = ps.exitValue();

if (i == 0) {

System.out.println("执行完成.") ;

} else {

System.out.println("执行失败.") ;

}

ps.destroy();

ps = null;

new Test().killProcess();

}

public void killProcess(){

Runtime rt = Runtime.getRuntime();

Process p = null;

try {

rt.exec("cmd.exe /C start wmic process where name='cmd.exe' call terminate");

} catch (IOException e) {

e.printStackTrace();

}

}

}

方式二

process.destroy();

cmd /c dir 是执行完dir命令后封闭命令窗口。

cmd /k dir 是执行完dir命令后不封闭命令窗口。

cmd /c start dir 会打开一个新窗口后执行dir指令,原窗口会封闭。

cmd /k start dir 会打开一个新窗口后执行dir指令,原窗口不会封闭。

怎么销毁java中的线程

thread可以用destroy方法销毁,但它不保证资源被释放,所以只能用在无资源的情况下。一般都自行控制代码逻辑让run()方法正常执行完,常用的方法包括可以被打断(在代码里检测interrupted())、检测状态标识退出循环等。

Java的JFrame怎么销毁啊?

JFrame有一个 public void dispose()可以撤销窗口,并且释放窗口使用的资源。直接调用就可以了,例如:

JFrame frame = new JFrame();

frame.dispose();

不过更加一般的方法是在创建窗体时调用frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);这样就可以直接点击关闭按钮时撤销窗体并且释放窗体所使用的资源。

java销毁方法的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java线程销毁方法、java销毁方法的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-13,除非注明,否则均为首码项目网原创文章,转载请注明出处。