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();
}
}
}




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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值