Threads and Anonymous Classes in JAVA
As we all know,a thread is a separate process on your computer.you can run multiple threads all at the same time.
multi-threaded code has the disadvantage of becoming quite complex very quickly,although java has some great classes for dealing
with multithreading and simplifying it.
Today we will just look at creating threads,along with using anonymous classes to simplify(or some would say,complexify)your code.
there are two methods to Creating Threads in Java
the first way is to extend the Thread class, override the run() method with the code you want to execute,
then create a new object from your class and call start().
the second method is to pass an implementation of the Runnable interface to the constructor of Thread,
then call start().
we will look at both of the method in turn.
1.Extending the Thread Class
the sample code ad bellow:
public class Worker extends Thread { @Override public void run() { // Loop for ten iterations. for(int i=0; i i++) { System.out.println(i + " looping ..."); // Sleep for a while try { Thread.sleep(200); } catch (InterruptedException e) { // Interrupted exception will occur if // the Worker objects interrupt() method // is called. interrupt() is inherited // from the Thread class. break; }As shown above, we have create a Worker class that extends Thread. And override the run() Method and put some code
in it. The code just loops repeatedly and outputs a method.
weve also used a static method of the thread class,sleep().
note:if you use sleep,you should have to catch InterruptedException.
In the code below,we create two worker class and call their inherited start() methods, both run at the same time,not one after the other
public class Application {the start() method, inherited from the parent Thread class, creates a new thread and runs whatever code is in run() in the new thread.
9 looping ...
if not use the threads:
public class Application {2.passing code to Thread Directly
the second method of starting a thread is to put the code you want to run in the run method of a class that implements the Runnable interface, then pass it to
the constructor of a thread class
the code below does exactly that;weve put the code all in one file to make it easier to follow.
class CodeRunner implements Runnable { @Override public void run() { // Loop for ten iterations. for(int i=0; i i++) { System.out.println(i + " looping ..."); // Sleep for a while try { Thread.sleep(200); } catch (InterruptedException e) { break; public class Application {we can simplify this code by calling new directly on our coderunner class,right where we create the thread.
9 looping ...Thread thread = new Thread(new CodeRunner()); thread.start();3.Quick and Dirty Thread using Anonymous Classin fact we can make this code even terser by creating a new intance of Runnable,sortof,directly in the Thread constructor.
Actually,we cant create a new intance of Runnable because its an interface; so we cant do the follow:
// Wont work Thread thread = new Thread(new Runnable()); thread.start();but we can get this to work if we add in some curly brackets and implement the missing run() method in them.// This works Thread thread = new Thread(new Runnable() { @Override public void run() { thread.start();of course, to get this code to do anything, we have to add some actual code to run.Thread thread = new Thread(new Runnable() { @Override public void run() { for(int i=0; i i++) { System.out.println(i + " looping ..."); thread.start();
0 looping ... 1 looping ... 2 looping ... 3 looping ... 4 looping ... 5 looping ... 6 looping ... 7 looping ... 8 looping ... 9 looping ...finally,we can make the code ecen more terse, if a little more cryptic,by not bothering to declare a variable to hold the thread class, and thenjust calling the stat() method on it directly.
new Thread(new Runnable() { @Override public void run() { for(int i=0; i i++) { System.out.println(i + " looping ..."); }).start();here just to make the code a bit clearer.if you are also interest in linux and android embed system,please connection with us in QQ group:139761394
17/11/24 05:08:44 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable 2017-11-24 21:20:25 1:什么叫失望,什么叫绝望。总之是一脸懵逼的继续...... 之前部署的hadoop都是hadoop-2.4.1.tar.gz,这几天换成了hadoop-2.6.4.tar.gz,部署老生常谈,启动就不一样了,本来吧,也就是warn,不是error,启动hdfs和yarn,节点都可以正常启动,但是对于我这种强迫症,能容忍下面这一大推错误吗?当你看到这篇的时候,显然是不能,虽然网上已经有很多了,但是貌似好多还是不好使。
相关文章
- Java基础(多态的理解与应用)
- eclipse怎么导入java文件_Eclipse如何导入JAVA工程?如何将项目导入Eclipse中?
- 【说站】java中@Override注解是什么
- 微信开发SDK java版,支持maven;微信Java开发工具包,支持包括微信支付、开放平台、公众号、企业微信/企业号、小程序等微信功能模块的后端开发。
- java中clone_java copyproperties
- 【错误记录】Java AWT 图形界面编程报错 ( Exception in thread “main“ java.awt.AWTError: BoxLayout can‘t be shared )
- 一个通用的Java正则匹配工具(检查邮箱名、电话号码、用户密码、邮政编码等合法性的工具类)详解手机开发
- Java内存溢出的常见情况和处理方式总结详解架构师
- Java随机生成常用汉字验证码详解编程语言
- Java操作Oracle数据库:实现数据库连接与访问(java连接oracle数据库)
- Linux下配置Java开发环境的指南(linux配置java环境)
- 操作MySQL中In操作指南(mysql中in)
- Java操作Redis实现超时处理(redisjava过期)
- Java神器:集成Redis,提高效率!(java集成redis)
- Java如何查询MySQL?25字(java查询mysql)
- 如何在Linux系统中正确配置Java?(linux下配置java)
- Java操作Redis实现数据快速存取(java访问redis)
- Java编程实现MySQL数据库连接(java连mysql数据库)
- 秘籍学习实现纯Java版Redis(纯java版redis)
- 异常Java程序捕获Oracle异常从失败中学习(java捕获oracle)
- Oracle如何优化IN查询以提高效率(oracle优化in查询)
- Oracle中IN与OR运算实现多条件查询的最佳选择(oracle中in与or)