ImageVerifierCode 换一换
格式:DOCX , 页数:41 ,大小:24.52KB ,
资源ID:6451024      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6451024.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java多线程机制示例.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java多线程机制示例.docx

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