java线程跟多线程

时间:2022-09-02 15:38:56

java创建线程两种方式:

1.继承Thread创建线程

/**
* Created by lsf on 16/4/18.
*/
class NewThread extends Thread {
NewThread(){
super(); //创建线程
start(); //启动线程
} public void run() {
long starttime = System.currentTimeMillis();
System.out.println("child thread..."+starttime);
}
} class CurrentThreadDemo {
public static void main(String args[]) {
long starttime2 = System.currentTimeMillis();
System.out.println("main thread,,,"+starttime2); //主线程
new NewThread();
System.out.println("住县城");
}
}

2.实现

import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;

import java.util.Date;

/**
* Created by lsf on 16/4/18.
*/
class NewThread extends Thread {
NewThread(){
Thread t = new Thread(this); //创建线程
t.start(); //启动线程
} public void run() {
long starttime = System.currentTimeMillis();
System.out.println("child thread..."+starttime);
}
} class CurrentThreadDemo {
public static void main(String args[]) {
long starttime2 = System.currentTimeMillis();
System.out.println("main thread,,,"+starttime2);
new NewThread();
System.out.println("主线程"); //主线程
}
}

3.给任务创建多个线程去执行

class NewThread extends Thread {
String name;
NewThread(String threadname){
name = threadname;
Thread t = new Thread(this,threadname); //创建线程
t.start(); //启动线程
} public void run() {
long starttime = System.currentTimeMillis();
System.out.println("child thread..."+name);
}
} class CurrentThreadDemo {
public static void main(String args[]) {
long starttime2 = System.currentTimeMillis();
System.out.println("main thread,,,"+starttime2);
new NewThread("demo1");
new NewThread("demo2");
new NewThread("demo3");
System.out.println("主线程"); //主线程
}
}

4.线程优先级设置

/**
* Created by lsf on 16/4/22.
*/ class ThreadTest implements Runnable { Thread t;
int count = 0;
private volatile Boolean flag = true; public ThreadTest(int p) {
t = new Thread(this);
t.setPriority(p);
} public void start(){
t.start();
} public void finish(){
flag = false;
} @Override
public void run() {
while(flag){
count++;
}
} } public class ThreadPriority {
public static void main(String[] args) {
ThreadTest t1 = new ThreadTest(Thread.NORM_PRIORITY - 2);
ThreadTest t2 = new ThreadTest(Thread.NORM_PRIORITY + 2);
t1.start();
t2.start();
t1.finish();
t2.finish();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} try {
System.out.println("t1 count:"+t1.count);
System.out.println("t2 count:"+t2.count);
t1.t.join();
t2.t.join();
System.out.println("t1 is alive:" + t1.t.isAlive());
System.out.println("t2 is alive:" + t1.t.isAlive());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

5.线程同步

线程同步的关键在于同一时刻线程在管程内,应用场景一般是:当某个方法(callme)需要用多线程去执行,可以改造一下对应的方法,加上关键词synchronized,这样在调用过程中,每个线程都会默认进入隐式管程。

/**
* Created by root on 16-4-15.
*/ class Callme {
synchronized void call(String msg){
System.out.println("["+msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("]");
}
} class ThreadCaller implements Runnable{ String msg;
Callme target;
Thread t; public ThreadCaller(Callme targ,String s) {
target = targ;
msg = s;
t = new Thread(this);
t.start();
} @Override
public void run() {
target.call(msg);
}
} class Demo {
public static void main(String[] args) {
Callme target = new Callme();
ThreadCaller obj1 = new ThreadCaller(target,"Hello");
ThreadCaller obj2 = new ThreadCaller(target,"Synchronized");
ThreadCaller obj3 = new ThreadCaller(target,"World"); try {
obj1.t.join();
obj2.t.join();
obj3.t.join();
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
}
}

  

java线程跟多线程的更多相关文章

  1. Java线程与多线程教程

    本文由 ImportNew - liken 翻译自 Journaldev.   Java线程是执行某些任务的轻量级进程.Java通过Thread类提供多线程支持,应用可以创建并发执行的多个线程. 应用 ...

  2. Java线程和多线程(十三)——Callable,Future,FutureTask

    在Java多线程之中,Callable和Future的使用时非常广泛的.在之前的文章中,我们了解了关于Java线程池基础的一些内容,知道如何提交Runnable的任务.但是,Runnable的任务是无 ...

  3. Java线程和多线程(十二)——线程池基础

    Java 线程池管理多个工作线程,其中包含了一个队列,包含着所有等待被执行的任务.开发者可以通过使用ThreadPoolExecutor来在Java中创建线程池. 线程池是Java中多线程的一个重要概 ...

  4. Java线程和多线程(三)——线程安全和同步

    线程安全在Java中是一个很重要的课题.Java提供的多线程环境支持使用Java线程.我们都知道多线程共享一些对象实例的话,可能会在读取和更新共享数据的事后产生数据不一致问题. 线程安全 之所以会产生 ...

  5. Java线程和多线程(一)——线程的基本概念

    Java 线程是一个轻量级执行任务的处理单元.Java提供了Thread类来支持多线程,开发者在应用中可以创建多个线程来支持并发执行任务. 在应用中存在两种类型的线程,用户线程和守护线程.当我们启动应 ...

  6. Java 线程与多线程

    Java是一门支持多线程的编程语言! 什么是进程? 计算机中内存.处理器.IO等资源操作都要为进程进行服务. 一个进程上可以创建多个线程,线程比进程更快的处理单元,而且所占用的资源也小,多线程的应用也 ...

  7. Java线程和多线程(八)——Thread Dump

    Java的Thread Dump就是列出JVM中所有激活状态的线程. Java Thread Dump Java Thread Dump在分析应用性能瓶颈和死锁的时候,是非常有效的. 下面将介绍多种不 ...

  8. Java 线程和多线程执行过程分析

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  9. Java线程和多线程(十五)——线程的活性

    当开发者在应用中使用了并发来提升性能的同时,开发者也需要注意线程之间有可能会相互阻塞.当整个应用执行的速度比预期要慢的时候,也就是应用没有按照预期的执行时间执行完毕.在本章中,我们来需要仔细分析可能会 ...

随机推荐

  1. C和指针 第十七章 二叉树删除节点

    二叉树的节点删除分为三种情况: 1.删除的节点没有子节点,直接删除即可 2. 删除的节点有一个子节点,直接用子节点替换既可以 3.删除的节点有两个子节点. 对于第三种情况,一般是不删除这个节点,而是删 ...

  2. Mysql如何向存在外键的数据表中插入数据

    1.创建表 CREATE TABLE `trn_comment_msg` ( `comMsgId` ) NOT NULL AUTO_INCREMENT COMMENT '评论消息主键', `msgId ...

  3. 查看mysql表结构的几种方法

    desc 表名; show columns from 表名; describe 表名; show create table 表名; use information_schemaselect * fro ...

  4. DataTable.ImportRow()与DataTable.Rows.Add()的区别

    今天写代码的时候用到ImportRow()向DataTable中添加记录,代码如下: DataTable dt = datatable;DataRow dr = dt.NewRow();dr[&quo ...

  5. wcf中 生成x5.09证书的工具

    原文链接http://blog.pluralsight.com/selfcert-create-a-self-signed-certificate-interactively-gui-or-progr ...

  6. 五、oracle 表的管理

    一.表名和列名的命名规则1).必须以字母开头2).长度不能超过30个字符3).不能使用oracle的保留字4).只能使用如下字符 a-z,a-z,0-9,$,#等 二.数据类型1).字符类char 长 ...

  7. Android 字体适配方案

    开发过程中,按照UI设计尺寸做好UI页面,当用户自定义自己的手机字体大小之后UI完全没法看了,这个时候就在想让app字体大小始终一致就好了 下面看一下,出现的问题和解决方案     做个简单的例子,先 ...

  8. linux buff/cache释放

    手动释放cache # > /proc/sys/vm/drop_caches

  9. ubuntu16.04 安装symfony3.3.11 碰到的问题:extension dom is required | oops an error occurred 500

    问题1:Uncaught exception 'RuntimeException' with message 'Extension DOM is required' 解决:sudo apt-get i ...

  10. python3 使用openpyxl库读写excel(续)

    官网:https://openpyxl.readthedocs.io/en/stable/