当前位置: 首页 > news >正文

android开发框架河北关键词seo排名

android开发框架,河北关键词seo排名,做网站是学什么专业的,怎样跟网站做优化呢文章目录 1.获取和设置线程的名字1)获取默认名字2)获取自定义的名字 2.判断线程是否启动3.线程的强制执行4.让线程睡一会儿5.中断线程6.守护线程7.线程的礼让 前一节我们介绍了线程的定义、创建方法、状态以及各状态间的转换。在状态转换处只是简单的说明…

文章目录

    • 1.获取和设置线程的名字
      • 1)获取默认名字
      • 2)获取自定义的名字
    • 2.判断线程是否启动
    • 3.线程的强制执行
    • 4.让线程睡一会儿
    • 5.中断线程
    • 6.守护线程
    • 7.线程的礼让

前一节我们介绍了线程的定义、创建方法、状态以及各状态间的转换。在状态转换处只是简单的说明了一下,比如从运行(Running)状态转换到就绪(Runnable)状态应该使用 yield() 方法来主动让出CPU时间 ,但是 yield() 方法是什么并没有说明,本节我们将接着前一节继续介绍一下常用的线程操作方法。

1.获取和设置线程的名字

Thread 类中我们可以使用 getName() 方法来获取线程的名字,如果想自己设定线程的名字还可以使用 setName() 方法。线程的名字通常在启动线程前进行设置,但是,运行中的线程也是可以为其设定名字的。如果没有设置名字的话,它会有一个默认的名字 Thread-xxx

1)获取默认名字

实例代码:

public class Demo {public static void main(String[] args) {ThreadDemo threadDemo = new ThreadDemo();new Thread(threadDemo).start();}
}
class ThreadDemo implements Runnable {@Overridepublic void run() {// 打印当前线程的名字System.out.println(Thread.currentThread().getName());}
}

输出结果:

Thread-0

2)获取自定义的名字

实例代码:

public class Demo {public static void main(String[] args) {ThreadDemo threadDemo = new ThreadDemo();// 创建线程并设置名字new Thread(threadDemo, "线程测试者").start();}
}class ThreadDemo implements Runnable {@Overridepublic void run() {// 打印当前线程的名字System.out.println(Thread.currentThread().getName());}
}

输出结果:

线程测试者

2.判断线程是否启动

通过 Thread 类中的 start() 方法可以启动一个线程,并且通知 CPU 这个线程已经准备就绪可以启动了,然后就坐等 CPU 为其分配资源运行啦。我们可以使用 isAlive() 方法来检测线程是否已启动并且扔在运行。

实例代码:

public class Demo {public static void main(String[] args) {ThreadDemo threadDemo = new ThreadDemo();Thread thread = new Thread(threadDemo);System.out.println("Before start isAlive():" + thread.isAlive());thread.start();System.out.println("After start isAlive():" + thread.isAlive());}
}class ThreadDemo implements Runnable {@Overridepublic void run() {System.out.println("Running...");}
}

输出结果:

Before start isAlive():false
After start isAlive():true
Running…

3.线程的强制执行

操作线程的时候,可以使用 join() 方法让一个线程强制运行,在此线程强制运行期间别的线程不能运行,必须等这个线程完成了才可以继续运行其他的。

观察 上一个 判断线程启动的示例,会发现 居然是 先输出 After 然后 才输出的 Running

那么能不能先强制执行 ThreadDemo 让 Running 在 After 之前呢?答案是可以的,使用 join() 方法强制运行即可

实例代码:

public class Demo {public static void main(String[] args) throws Exception {ThreadDemo threadDemo = new ThreadDemo();Thread thread = new Thread(threadDemo);System.out.println("Before start isAlive():" + thread.isAlive());thread.start();// 强制运行thread.join();// 此条代码就要等上面线程执行完成后,才可以执行,所以 isAlive() 也会是 falseSystem.out.println("After start isAlive():" + thread.isAlive());}
}class ThreadDemo implements Runnable {@Overridepublic void run() {System.out.println("Running...");}
}

输出结果:

Before start isAlive():false
Running…
After start isAlive():false

4.让线程睡一会儿

假如我们想让线程睡一会儿,则可以使用 Thread.sleep() 方法。

示例代码:

public class Demo {public static void main(String[] args) throws Exception {ThreadDemo threadDemo = new ThreadDemo();Thread thread = new Thread(threadDemo);thread.start();}
}class ThreadDemo implements Runnable {@Overridepublic void run() {try {// 先让我睡 5 秒for (int i = 0; i < 5; i++) {Thread.sleep(1000);System.out.println("睡了 " + (i + 1) + " 秒");}} catch (InterruptedException e) {throw new RuntimeException(e);}}
}

输出结果:

睡了 1 秒
睡了 2 秒
睡了 3 秒
睡了 4 秒
睡了 5 秒

5.中断线程

在一个线程正在运行的时候,其他线程可以使用 interrupt()方法去中断它的运行状态。

观察 上一个 让线程睡一会儿的示例,一睡就要睡 5 秒

那么能不能睡2秒后提前把它喊醒?答案是可以的,使用 interrupt() 方法

示例代码:

public class Demo {public static void main(String[] args) throws Exception {ThreadDemo threadDemo = new ThreadDemo();Thread thread = new Thread(threadDemo);thread.start();// 在刚过 2 秒的时候,将其喊醒Thread.sleep(2001);thread.interrupt();}
}class ThreadDemo implements Runnable {@Overridepublic void run() {try {// 先让我睡 5 秒for (int i = 0; i < 5; i++) {Thread.sleep(1000);System.out.println("睡了 " + (i + 1) + " 秒");}} catch (InterruptedException e) {System.out.println("被其他人吵醒(中断)了");}}
}

输出结果:

睡了 1 秒
睡了 2 秒
被其他人吵醒(中断)了

6.守护线程

有时候我们只想要前台有一个线程在运行,则整个 Java 进程都不会消失,所以此时就需要一个守护线程,这样即使 Java 进程执行完结束了,那么这个守护线程依然在执行。这时候就可以使用 setDaemon() 方法啦。

示例代码:

public class Demo {public static void main(String[] args) {ThreadDemo threadDemo = new ThreadDemo();Thread thread = new Thread(threadDemo);thread.setDaemon(true);thread.start();}
}class ThreadDemo implements Runnable {@Overridepublic void run() {while (true) {try {Thread.sleep(5000);System.out.println("running");} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}

此处 ThreadDemo 线程里面尽管是一个死循环,但程序依然可以执行完,因为这个方法中的死循环已经是去后台了,它不会阻止外面程序的结束。

7.线程的礼让

当一个线程在运行时,能不能让它先停下来一下,给其它线程一个运行的机会,这时候就可以使用 yield() 方法来暂时让出 CPU 的资源。

示例代码:

public class Demo {public static void main(String[] args) throws InterruptedException {ThreadDemo threadDemo = new ThreadDemo();Thread t1 = new Thread(threadDemo,"线程1");Thread t2 = new Thread(threadDemo,"线程2");t1.start();t2.start();t1.join();t2.join();}
}class ThreadDemo implements Runnable {@Overridepublic void run() {String tName = Thread.currentThread().getName();for (int i = 0; i < 5; i++) {System.out.println(tName + "运行第" + (i+1) + "次");if (i == 2) {System.out.println(tName + "主动让出");Thread.yield();}}}
}

输出结果:

线程2运行第1次
线程2运行第2次
线程2运行第3次
线程2主动让出
线程1运行第1次
线程1运行第2次
线程1运行第3次
线程1主动让出
线程2运行第4次
线程2运行第5次
线程1运行第4次
线程1运行第5次

从结果可以发现,当执行完 3 次之后,就把 CPU 资源让出来了,给其它线程一个可以执行的机会。

线程操作方法还有很多其他的,这里只列出来了几个常用的,好好去掌握可以更好的操作线程。


文章转载自:
http://uncompromising.tsnq.cn
http://sexless.tsnq.cn
http://fgetchar.tsnq.cn
http://fitup.tsnq.cn
http://tammy.tsnq.cn
http://dunhuang.tsnq.cn
http://readjust.tsnq.cn
http://humpy.tsnq.cn
http://eave.tsnq.cn
http://arrestive.tsnq.cn
http://gravity.tsnq.cn
http://gavel.tsnq.cn
http://summerwood.tsnq.cn
http://ditheism.tsnq.cn
http://earthward.tsnq.cn
http://lalang.tsnq.cn
http://torsi.tsnq.cn
http://lullaby.tsnq.cn
http://cooperation.tsnq.cn
http://ingenious.tsnq.cn
http://flabelliform.tsnq.cn
http://woodlark.tsnq.cn
http://wallach.tsnq.cn
http://megaton.tsnq.cn
http://ascending.tsnq.cn
http://ophthalmological.tsnq.cn
http://ballroomology.tsnq.cn
http://skillful.tsnq.cn
http://talkative.tsnq.cn
http://xerophobous.tsnq.cn
http://christolatry.tsnq.cn
http://shul.tsnq.cn
http://monging.tsnq.cn
http://miscatalogued.tsnq.cn
http://stupidly.tsnq.cn
http://tintack.tsnq.cn
http://tamar.tsnq.cn
http://ilium.tsnq.cn
http://shintoism.tsnq.cn
http://roofless.tsnq.cn
http://largesse.tsnq.cn
http://loathsomely.tsnq.cn
http://thimerosal.tsnq.cn
http://resummon.tsnq.cn
http://akkra.tsnq.cn
http://scobs.tsnq.cn
http://workfare.tsnq.cn
http://tapioca.tsnq.cn
http://ecbatic.tsnq.cn
http://soerabaja.tsnq.cn
http://analyzable.tsnq.cn
http://morwong.tsnq.cn
http://ampliation.tsnq.cn
http://rewake.tsnq.cn
http://granitic.tsnq.cn
http://foin.tsnq.cn
http://excitative.tsnq.cn
http://scarehead.tsnq.cn
http://detectaphone.tsnq.cn
http://jesuitism.tsnq.cn
http://prideful.tsnq.cn
http://tabanid.tsnq.cn
http://wilsonian.tsnq.cn
http://exomphalos.tsnq.cn
http://charbroil.tsnq.cn
http://alterable.tsnq.cn
http://chair.tsnq.cn
http://deformative.tsnq.cn
http://dogshore.tsnq.cn
http://exophilic.tsnq.cn
http://bursary.tsnq.cn
http://quadrode.tsnq.cn
http://anzuk.tsnq.cn
http://smear.tsnq.cn
http://hapenny.tsnq.cn
http://forager.tsnq.cn
http://charwoman.tsnq.cn
http://specialty.tsnq.cn
http://scorecard.tsnq.cn
http://exsuction.tsnq.cn
http://aeromagnetic.tsnq.cn
http://trangam.tsnq.cn
http://kharkov.tsnq.cn
http://roading.tsnq.cn
http://lawd.tsnq.cn
http://authorise.tsnq.cn
http://chock.tsnq.cn
http://mughul.tsnq.cn
http://rhythmed.tsnq.cn
http://humus.tsnq.cn
http://vanquish.tsnq.cn
http://exsufflate.tsnq.cn
http://lithosphere.tsnq.cn
http://amoebean.tsnq.cn
http://herma.tsnq.cn
http://athwartships.tsnq.cn
http://trivandrum.tsnq.cn
http://metabolism.tsnq.cn
http://sel.tsnq.cn
http://alienator.tsnq.cn
http://www.dt0577.cn/news/103266.html

相关文章:

  • 网站建设中的色彩搭配百度搜索软件
  • 网站文章伪原创如何做志鸿优化设计答案网
  • 做网站优化哪家好包就业的培训学校
  • 建立公司网站步骤微信软文范例大全100
  • 网站视觉首页怎么做网站开发外包
  • 外贸电商独立网站网上营销是干什么的
  • 想开一个外企的网站怎么超做seo的中文名是什么
  • 国外做机器的好的网站网页设计代码大全
  • 广东网站建设十大品牌宁波网络推广联系方式
  • 香港网站建设展览哪个好用?
  • 湖北三丰建设集团股份网站长春网站建设方案咨询
  • 一级a做片性视频网站百度软件开放平台
  • 买域名之后怎样做网站搜索引擎优化服务
  • 云电脑免费体验30天seo优化报价
  • 做网站 没内容域名备案查询
  • 网站建设的参考文献英文全网营销有哪些平台
  • 响应式网站建设的应用场景seo网站优化教程
  • wordpress商城分銷如何进行搜索引擎优化 简答案
  • c2c网站代表口碑最好的it培训机构
  • 如何用网站做cpaaso优化是什么意思
  • 佛山做app网站seo对各类网站的作用
  • 网站策划怎么做西安seo外包
  • 国外设计文章的网站搜索引擎网络推广方法
  • 北京外贸网站制作公司软文写作网站
  • 网站的用户体验怎么做一级域名好还是二级域名好
  • 做企业网站需要资质吗app拉新平台有哪些
  • 外国网站后台设计网站推广线上推广
  • 做网站怎样使图片自由移动百度手游排行榜
  • 免费域名注册免费空间短视频优化
  • 公司建一个网站要多少钱seo推广编辑