java基础进阶(文件列表,线程,线程组)编程实例(4篇)
???? 此處刊登代碼均測試通過,完全準確!
import java.io.*;
public class DirList{
public static void main(String[] args){
try{
File path=new File(".");
/*“.”指當前目錄*/
String[] list;
if(args.length==0)
list=path.list();/*列出當前文件*/
else
?list=path.list(new DirFilter(args[0]));
for(int i=0;i<list.length;i++)
System.out.println(list[i]);
}
catch(Exception e){
e.printStackTrace();
}
}
static class DirFilter implements FilenameFilter{
String afn;
DirFilter(String afn){this.afn=afn;}
public boolean accept(File dir,String name)/*name是文件名*/{
String f=new File(name).getName();
/*getName得到文件名的非目錄部分,只有文件名*/
return f.indexOf(afn)!=-1;
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
public class SimpleRunnable implements Runnable{
private String message;
public static void main(String[] args){
SimpleRunnable r1=new SimpleRunnable("Hello");
Thread t1=new Thread(r1);
t1.start();
for(;;)/*死循環(huán)*/{
System.out.println("Bye-bye");
}
}
public SimpleRunnable(String message){
this.message=message;
}
public void run(){
for (;;)/*死循環(huán)*/{
System.out.println(message);
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
public class MethodText{
public static void main(String[] args){
FirstThread first=new FirstThread();
SecondThread second=new SecondThread();
first.start();
second.start();
try{
System.out.println("Waiting for first thread to finish!");
first.join();
System.out.println("It's a long wait!");
System.out.println("Waking up second thread...");
second.resume();
System.out.println("Waiting for second thread to finish!");
second.join();
}catch(InterruptedException e){
}
System.out.println("I'm ready to finish too.");
}
}
class FirstThread extends Thread{
public void run(){
try{
System.out.println("First thread starts running!");
sleep(10000);
System.out.println("First thread finishes running!");
}catch(InterruptedException e){
}
}
}
class SecondThread extends Thread{
public void run(){
System.out.println("Second thread starts running.");
System.out.println("Second thread suspend itself.");
suspend();
System.out.println("Second thread runs again and finishes.");
}
}
---------------------------------------------------------------------------------------------------------------------------------------
public class Grp implements Runnable{
public void run(){
for (;;){
System.out.println("thread"+Thread.currentThread().getName());
try{
Thread.sleep(500);
}catch(Exception e){
}
}
}
public static void main(String[] args)
{
ThreadGroup g=new ThreadGroup("My Group");
Runnable r=new Grp();
Thread t=new Thread(g,r);
t.start();
t=new Thread(g,r);
t.start();
for(;;){
try{
Thread.sleep(5000);
}catch(Exception e){
}
g.suspend();
System.out.println("thread"+Thread.currentThread().getName());
try{
Thread.sleep(5000);
}catch(Exception e){
}
g.resume();
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
轉(zhuǎn)載于:https://www.cnblogs.com/shiyangxt/archive/2008/06/09/1216339.html
總結(jié)
以上是生活随笔為你收集整理的java基础进阶(文件列表,线程,线程组)编程实例(4篇)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 永真_sql注入
- 下一篇: Springboot+vue项目酒店民宿