`

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(;;)/*死循环*/{
System.out.println("Bye-bye");
}
}
public SimpleRunnable(String message){
this.message=message;
}
public void run(){
for (;;)/*死循环*/{
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();
}
}
}




---------------------------------------------------------------------------------------------------------------------------------------

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics