1、Java多线程机制示例Java多线程机制(示例).txt“恋”是个很强悍的字。它的上半部取自“变态”的“变”,下半部取自“变态”的“态”。1 Java多线程机制 1.1基本概念线程控制方法public class Thread implements Runnable public final static int MIN_PRIORITY; public final static int NORM_PRIORITY; public final static int MAX_PRIORITY; public Thread(); public Thread(Runnable target); pu
2、blic Thread(ThreadGroup group, Runnable target); public Thread(String name); public Thread(ThreadGroup group, String name); public Thread(Runnable target, String name); public Thread(ThreadGroup group, Runnable target, String name); public void run(); public synchronized native void start(); public
3、final void stop(); public final synchronized void stop(Throwable o); public static native void yield(); public static native void sleep(long millis) throws InterruptedException; public static void sleep(long millis, int nanos) throws InterruptedException public final void suspend(); public final voi
4、d resume(); public final synchronized void join(long millis) throws InterruptedException; public final synchronized void join(long millis, int nanos)throws InterruptedException; public final void join() throws InterruptedException; public void interrupt(); public static boolean interrupted(); public
5、 boolean isInterrupted(); public void destroy(); public final native boolean isAlive(); public final void setPriority(int newPriority); public final int getPriority(); public final void setName(String name); public final String getName(); public final ThreadGroup getThreadGroup(); public static nati
6、ve Thread currentThread(); public static int activeCount(); public static int enumerate(Thread tarray); public native int countStackFrames(); public static void dumpStack(); public final void setDaemon(boolean on); public final boolean isDaemon(); public void checkAccess(); public String toString();
7、_1.2多线程实现方法 例1:下面程序演示如何用生成Thread子类的方法来创建新线程。/ThreadTest1.javapublic class ThreadTest1 ThreadTest1() FirstThread first = new FirstThread(); SecondThread second = new SecondThread(); first.start(); second.start(); public static void main(String args) new ThreadTest1(); class FirstThread extends Thread
8、 public void run() try System.out.println(First thread starts running.); for(int i=0; i10; i+) System.out.println(First + i); sleep(1000); System.out.println(First thread finishes running.); catch (InterruptedException e) class SecondThread extends Thread public void run() try System.out.println(tSe
9、cond thread starts running.); for(int i=0; i10; i+) System.out.println(tSecond + i); sleep(1000); System.out.println(tSecond thread finishes running.); catch (InterruptedException e) 程序设计了两个线程FirstThread类和SecondThread类,它们都是Thread类 的子类覆盖了run()方法,在其中分别进行打印数值的工作。除了这两个线程 外,还有一个线程在执行,就是启动类线程,称它为主线程,它负责生成
10、另外 两个线程,再用start()方法启动这两个线程。线程first和second启动后,并发执行。观察执行结果会发现两个线程交替打印数据,而不是一个线程完成了所有打印工作后,另一个线程才开始打印工作,这就是多线程的本质。 提示:线程在调用Thread类方法sleep()睡眠时,有可能产生异常,要求 应用程序用try-catch捕获这个异常,如果不用try-catch,程序将出错。 某次的运行结果:First thread finishes running.First 0 Second thread starts running. Second 0 Second 1First 1First 2
11、 Second 2First 3 Second 3First 4 Second 4 Second 5First 5First 6 Second 6First 7 Second 7First 8 Second 8First 9 Second 9First thread finishes running. Second thread finishes_ 例2:下面程序演示如何用生成Thread子类的方法来创建新线程。/ThreadTest2.javapublic class ThreadTest2 ThreadTest2() FirstThread first = new FirstThread(
12、); SecondThread second = new SecondThread(); first.start(); second.start(); try first.join(); System.out.println(Waiting for first thread to finish.); System.out.println(Waking up second thread.); second.resume(); catch (InterruptedException e) public static void main(String args) new ThreadTest2();
13、 class FirstThread extends Thread public void run() try System.out.println(First thread STARTS running.); for(int i=0; i10; i+) System.out.println(First + i); sleep(1000); System.out.println(First thread FINISHES running.); catch (InterruptedException e) class SecondThread extends Thread public void
14、 run() try System.out.println(tSecond thread STARTS running.); for(int i=0; i10; i+) if(i= 4) suspend(); System.out.println(tSecond + i); sleep(1000); System.out.println(tSecond thread FINISHES running.); catch (InterruptedException e) 程序仍然使用两个线程打印数据,不同的是second线程在打印数据过程中,发现是数值4,则调用suspend()方法,暂停本身的执
15、行。主线程用join()方法等线程first执行结束后,用resume()方法来唤醒second线程,second线程被唤醒后,将继续完成打印工作。提示:join()也将出现InterruptedException异常,所以必须捕获异常。某次的运行结果:First thread STARTS running.First 0 Second thread STARTS running. Second 0First 1 Second 1First 2 Second 2First 3 Second 3First 4First 5First 6First 7First 8First 9First thr
16、ead FINISHES running.Waiting for first thread to finish.Waking up second thread. Second 4 Second 5 Second 6 Second 7 Second 8 Second 9 Second FINISHES finishes_例3:下面的程序说明如何用接口来创建线程。 /RunTest.javapublic class RunTest RunTest() FirstThread first = new FirstThread(); SecondThread second = new SecondThr
17、ead(); Thread thread1 = new Thread(first); Thread thread2 = new Thread(second); thread1.start(); thread2.start(); public static void main(String args) new RunTest(); class FirstThread implements Runnable public void run() try System.out.println(First thread starts running.); for(int i=0; i10; i+) Sy
18、stem.out.println(First + i); Thread.sleep(1000); System.out.println(First thread finishes running.); catch (InterruptedException e) class SecondThread implements Runnable public void run() try System.out.println(tSecond thread starts running.); for(int i=0; i10; i+) System.out.println(tSecond + i);
19、Thread.sleep(1000); System.out.println(tSecond thread finishes running.); catch (InterruptedException e) 这个程序与ThreadTest1.java有相同的功能,只不过现在用实现Runnable接口的方法来创建和执行线程。 _2 输入输出流类2.1文件系统1文件路径和属性例子:下面的程序首先判断给定文件是否存在,如果存在则显示文件路径、绝对路径等,然后再查询文件的属性。 /FileTest1.javaimport java.io.*;class FileTest1 public static
20、 void main(String args) String path; if(args.length != 1) System.err.println(Usage: java FileTest1 File or Dir); System.exit(-1); File f = new File(args0); if (f.exists() System.out.println(- ); System.out.println(Absolute Path: + f.getAbsolutePath(); System.out.println(File Path: + f.getPath(); Sys
21、tem.out.println(File Name: + f.getName(); System.out.println(Parent Dirtory: + f.getParent(); System.out.println(- ); String canRead = f.canRead() ? Yes : No; String canWrite = f.canWrite() ? Yes : No; String isFile = f.isFile() ? Yes : No; String isDir = f.isDirectory() ? Yes : No; String isAbs = f
22、.isAbsolute() ? Yes : No; System.out.println(Readable: +canRead); System.out.println(Writable: +canWrite); System.out.println(Is directoty: +isDir); System.out.println(Is file: +isFile); System.out.println(Is absolute path: +isAbs); else System.out.println(Cannot found file: + args0); 这是一个Applicatio
23、n程序,用命令行参数接受输入文件名。读者可以用不同的命令行参数来执行上面的程序,体会上述方法的区别。运行:java FileTest1 d:javafiletest1运行结果:- Absolute Path: d:javafiletest1 File Path: d:javafiletest1File Name: filetest1Parent Dirtory: d:java-Readable: Yes Writable: YesIs directoty: YesIs file: NoIs absolute path:Yes运行:java FileTest1 javafiletest1运行结果
24、:- Absolute Path: d:javafiletest1 File Path: javafiletest1File Name: filetest1Parent Dirtory: java-Readable: Yes Writable: YesIs directoty: YesIs file: NoIs absolute path:No运行: java FileTest1 javafiletest1filetest1.java运行结果:- Absolute Path: d:javafiletest1filetest1.java File Path: javafiletest1filet
25、est1.javaFile Name: filetest1.javaParent Dirtory: javafiletest1-Readable: Yes Writable: YesIs directoty: NoIs file: YesIs absolute path:No运行:java FileTest1 java运行结果:- Absolute Path: d:javaFile Path: javaFile Name: javaParent Dirtory: -Readable: Yes Writable: YesIs directoty: YesIs file: NoIs absolut
26、e path:No运行:java FileTest1 java运行结果:- Absolute Path: d:File Path: File Name: Parent Dirtory: null-Readable: Yes Writable: YesIs directoty: YesIs file: NoIs absolute path:No2创建目录和删除文件例2:/FileTest2.javaimport java.io.*;class FileTest2 public static void main(String args) File dir, subdir; if(args.length != 1) System.err.println(Usage: java FileTest2 ); System.exit(-1); dir = new File(args0); if(dir.exists() System.out.println(dir.getPath() + already exist!); else if(dir.mkdirs() /说明区别 System.out.println(Created directory: + dir.getAbsolutePath
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1