Thread and File
来源:互联网 发布:linux 安装jdk tar.gz 编辑:程序博客网 时间:2024/05/05 19:22
线程!
垃圾回收是线程
extends Thread
重写run()方法。
调用start()方法,启动线程。
Thread.currentThread(); 获取当前线程
thread.getName();获取线程的名字
thread.getId();获取线程的ID
thread.setPriority(10);设置权限从1-10;
守护线程。mythread.setDaemon(true);
主线程结束之后 守护线程紧接着停止运行。做一些类似于垃圾回收的事情。进度条里可使用。,一般都是死循环。
线程打断,mythread.interrupt();//不建议使用,而且有可能出现异常
public class MyThread extends Thread {private boolean isRunning = true;@Overridepublic void run() {//System.out.println("MyThread");//Thread thread = Thread.currentThread();//System.out.println(thread.getName());//System.out.println(thread.getId());while (isRunning) {//在里面停止线程比较好}for (int i = 0; i < 1000; i++) { System.out.println("thread第"+i + "个");}}}
mythread.notify();唤醒wait() 的线程
使用的时候需要加锁Synchronized
synchronized 的方法,需要相同的对象才有用,
不同的对象执行方法没用。
多个对象启动,需要static修饰锁方法
同步方法,非静态的对象是this,静态的是类的对象.
只有Thread对象才能启动线程
一个Runnable接口可以启动多个线程。
new Thread(Runnable对象).start();
Runnable里的方法不需要加static修饰,一个实验接口类的对象可以启动多个线程。
生产者-消费者例子中。当有一个生产者,一个消费者时,用boolean[]数组进行判断生产者结束时消费者消费完退出循环结束。
当有多个生产者是用Set 来存放生产者对象。结束时取出放入的对象。
volatile 善变变量。用来修饰变量 。
ThreadLocal<>()用存储数据 set()方法只能放入一个数据。在一个线程放入数据,只能在一个线程里取出数据。 再放入就覆盖之前的。
只能在本线程访问。
所有的线程运行在进程当中。
Runtime当前的进行时。进程结束时调用
Runtime.getRuntime();runtime.addShutdownHook();
Runtime runtime = Runtime.getRuntime();runtime.addShutdownHook(new Thread(){@Overridepublic void run() {System.out.println("虚拟机关闭");}});
File
File file = new File(".");//当前目录file.exists();//判断文件是否存在file.isFile();//是否是文件file.isDirectory();//是目录File[] files = file.listFiles();//获取当前目录下的所有文件for (File f : files) {System.out.println(f.getAbsolutePath());//目录下的路径}File file1 = new File("res/xml/a");System.out.println(file1.exists());System.out.println(file1.mkdirs());//创建目录System.out.println(file1.exists());
写的比较乱。原谅自己。
- Thread and File
- file lock and thread sync operations (I)
- Thread-safe caching object with file and HTTP implementations
- Thread Id and Thread Handle
- Thread Synchronization - UI Thread and Worker Thread
- cpp thread and tip
- Symbian Thread and Process
- thread and signal
- Thread - wait() and notifyAll()
- Symbian Thread and Process
- Thread and Runnable
- Process and Thread
- Handler and Thread
- Handler and Thread
- thread and fork
- thread and share variable
- Thread and Event
- android process and thread
- android中MVC,MVP和MVVM三种模式详解析
- JAVA第一个小项目——五子棋的制作(第二篇)
- PHP怎么获得当日零点的时间戳
- Android屏幕适配(三)
- android几种布局组件的常用属性
- Thread and File
- Android 多个activity之间的切换(跳转)
- css垂直居中和水平居中方法总结
- Uva11988 Broken Keyboard 链表
- ElasticSearch for GIS应用
- 查询表,这些表的外键引用于某个表的字段
- Android—SQlite数据库提高篇之数据库分页查询
- 车辆运动轨迹数据集
- 小火山的围棋梦想