多线程笔记1
1 直接继承thread类,覆写RUN方法,启动调用的时候用start();
package com.Thread;
class MyThread extends Thread{ // 继承Thread类,作为线程的实现类
private String name
; // 表示线程的名称
public MyThread(String name){
this.name = name ; //
通过构造方法配置name属性
}
public void run(){ // 覆写run()方法,作为线程 的操作主体
for(int
i=0;i<10;i++){
System.out.println(name + "运行,i = " + i)
;
}
}
};
public class ThreadDemo02{
public static void
main(String args[]){
MyThread mt1 = new MyThread("线程A ") ; //
实例化对象
MyThread mt2 = new MyThread("线程B ") ; // 实例化对象
mt1.start() ; //
调用线程主体
mt2.start() ; // 调用线程主体
}
};
2 实现runnable接口的方法
class MyThread1 implements Runnable{ //
实现Runnable接口,作为线程的实现类
private String name ; // 表示线程的名称
public
MyThread1(String name){
this.name = name ; //
通过构造方法配置name属性
}
public void run(){ // 覆写run()方法,作为线程 的操作主体
for(int
i=0;i<10;i++){
System.out.println(name + "运行,i = " + i)
;
}
}
};
public class RunnableDemo01{
public static void
main(String args[]){
MyThread1 mt1 = new MyThread1("线程A ") ; //
实例化对象
MyThread1 mt2 = new MyThread1("线程B ") ; // 实例化对象
Thread t1 =
new Thread(mt1) ; // 实例化Thread类对象
Thread t2 = new Thread(mt2) ; //
实例化Thread类对象
t1.start() ; // 启动多线程
t2.start() ; //
启动多线程
}
};
3 如果要实现资源的共享,则必须实现runnable方法
class MyThread3 implements Runnable{ //
继承Thread类,作为线程的实现类
private int ticket = 5 ; // 表示一共有5张票
public void
run(){ // 覆写run()方法,作为线程 的操作主体
for(int
i=0;i<100;i++){
if(this.ticket>0){
System.out.println("卖票:ticket
= " + ticket--) ;
}
}
}
};
public class ShareResource
{
public static void main(String args[]){
MyThread3 mt = new
MyThread3() ; // 实例化对象
new Thread(mt).start() ; // 调用线程主体
new
Thread(mt).start() ; // 调用线程主体
new Thread(mt).start() ; //
调用线程主体
}
};
4 设置线程的名称
class MyThread implements Runnable{ // 实现Runnable接口
public
void run(){ // 覆写run()方法
for(int
i=0;i<3;i++){
System.out.println(Thread.currentThread().getName()
+
"运行,i = " + i) ; // 取得当前线程的名字
}
}
};
public class
ThreadNameDemo{
public static void main(String args[]){
MyThread mt =
new MyThread() ; // 实例化Runnable子类对象
new Thread(mt).start() ; //
系统自动设置线程名称
new Thread(mt,"线程-A").start() ; // 手工设置线程名称
new
Thread(mt,"线程-B").start() ; // 手工设置线程名称
new Thread(mt).start() ; //
系统自动设置线程名称
new Thread(mt).start() ; // 系统自动设置线程名称
}
};
5 JAVA运行时至少启动了两个线程,一个是MAIN,一个是垃圾收集线程
6 线程的强制运行
class MyThread
implements Runnable{ // 实现Runnable接口
public void run(){ //
覆写run()方法
for(int
i=0;i<50;i++){
System.out.println(Thread.currentThread().getName()
+
"运行,i = " + i) ; // 取得当前线程的名字
}
}
};
public class
ThreadJoinDemo{
public static void main(String args[]){
MyThread mt =
new MyThread() ; // 实例化Runnable子类对象
Thread t = new Thread(mt,"线程"); //
实例化Thread对象
t.start() ; // 启动线程
for(int
i=0;i<50;i++){
if(i>10){
try{
t.join() ; //
线程强制运行
}catch(InterruptedException
e){}
}
System.out.println("Main线程运行 --> " + i)
;
}
}
};
输出:
线程运行,i = 0
Main线程运行 --> 0
线程运行,i =
1
Main线程运行 --> 1
线程运行,i = 2
Main线程运行 --> 2
线程运行,i =
3
Main线程运行 --> 3
线程运行,i = 4
线程运行,i = 5
Main线程运行 -->
4
线程运行,i = 6
Main线程运行 --> 5
线程运行,i = 7
Main线程运行 -->
6
线程运行,i = 8
Main线程运行 --> 7
线程运行,i = 9
Main线程运行 -->
8
线程运行,i = 10
Main线程运行 --> 9
Main线程运行 --> 10
线程运行,i =
11
线程运行,i = 12
线程运行,i = 13
可以看到,当I>10时,主线程停了下来,让T强制运行。
7 后台线程
class MyThread implements Runnable{ // 实现Runnable接口
public void
run(){ //
覆写run()方法
while(true){
System.out.println(Thread.currentThread().getName()
+ "在运行。") ;
}
}
};
public class ThreadDaemonDemo{
public
static void main(String args[]){
MyThread mt = new MyThread() ; //
实例化Runnable子类对象
Thread t = new Thread(mt,"线程"); //
实例化Thread对象
t.setDaemon(true) ; // 此线程在后台运行
t.start() ; //
启动线程
}
};
8 线程的优先级
class MyThread implements Runnable{ // 实现Runnable接口
public
void run(){ // 覆写run()方法
for(int
i=0;i<5;i++){
try{
Thread.sleep(500) ; //
线程休眠
}catch(InterruptedException
e){}
System.out.println(Thread.currentThread().getName()
+ "运行,i =
" + i) ; // 取得当前线程的名字
}
}
};
public class
ThreadPriorityDemo{
public static void main(String args[]){
Thread t1 =
new Thread(new MyThread(),"线程A") ; // 实例化线程对象
Thread t2 = new Thread(new
MyThread(),"线程B") ; // 实例化线程对象
Thread t3 = new Thread(new MyThread(),"线程C")
; // 实例化线程对象
t1.setPriority(Thread.MIN_PRIORITY) ; //
优先级最低
t2.setPriority(Thread.MAX_PRIORITY) ; //
优先级最低
t3.setPriority(Thread.NORM_PRIORITY) ; // 优先级最低
t1.start() ; //
启动线程
t2.start() ; // 启动线程
t3.start() ; // 启动线程
}
};
9
线程的礼让
class MyThread implements Runnable{ // 实现Runnable接口
public void
run(){ // 覆写run()方法
for(int
i=0;i<5;i++){
try{
Thread.sleep(500) ;
}catch(Exception
e){}
System.out.println(Thread.currentThread().getName()
+ "运行,i =
" + i) ; // 取得当前线程的名字
if(i==2){
System.out.print("线程礼让:")
;
Thread.currentThread().yield() ; //
线程礼让
}
}
}
};
public class ThreadYieldDemo{
public
static void main(String args[]){
MyThread my = new MyThread() ; //
实例化MyThread对象
Thread t1 = new Thread(my,"线程A") ;
Thread t2 = new
Thread(my,"线程B") ;
t1.start() ;
t2.start() ;
}
};
10
同步:
class MyThread implements Runnable{
private int ticket = 5 ; //
假设一共有5张票
public void run(){
for(int
i=0;i<100;i++){
synchronized(this){ //
要对当前对象进行同步
if(ticket>0){ //
还有票
try{
Thread.sleep(300) ; //
加入延迟
}catch(InterruptedException e){
e.printStackTrace()
;
}
System.out.println("卖票:ticket = " + ticket--
);
}
}
}
}
};
public class SyncDemo02{
public
static void main(String args[]){
MyThread mt = new MyThread() ; //
定义线程对象
Thread t1 = new Thread(mt) ; // 定义Thread对象
Thread t2 = new
Thread(mt) ; // 定义Thread对象
Thread t3 = new Thread(mt) ; //
定义Thread对象
t1.start() ;
t2.start() ;
t3.start() ;
}
};
11 出现死锁的例子
class Zhangsan{ // 定义张三类
public void
say(){
System.out.println("张三对李四说:“你给我画,我就把书给你。”") ;
}
public void
get(){
System.out.println("张三得到画了。") ;
}
};
class Lisi{ //
定义李四类
public void say(){
System.out.println("李四对张三说:“你给我书,我就把画给你”")
;
}
public void get(){
System.out.println("李四得到书了。")
;
}
};
public class ThreadDeadLock implements Runnable{
private
static Zhangsan zs = new Zhangsan() ; // 实例化static型对象
private static Lisi
ls = new Lisi() ; // 实例化static型对象
private boolean flag = false ; //
声明标志位,判断那个先说话
public void run(){ //
覆写run()方法
if(flag){
synchronized(zs){ // 同步张三
zs.say()
;
try{
Thread.sleep(500) ;
}catch(InterruptedException
e){
e.printStackTrace()
;
}
synchronized(ls){
zs.get()
;
}
}
}else{
synchronized(ls){
ls.say()
;
try{
Thread.sleep(500) ;
}catch(InterruptedException
e){
e.printStackTrace()
;
}
synchronized(zs){
ls.get()
;
}
}
}
}
public static void main(String
args[]){
ThreadDeadLock t1 = new ThreadDeadLock() ; //
控制张三
ThreadDeadLock t2 = new ThreadDeadLock() ; // 控制李四
t1.flag =
true ;
t2.flag = false ;
Thread thA = new Thread(t1) ;
Thread thB
= new Thread(t2) ;
thA.start() ;
thB.start() ;
}
};
12 生产者消费者的情况描述例子:
package com.Thread;
class Info{ // 定义信息类
private String name = "李兴华"; //
定义name属性
private String content = "JAVA讲师" ; // 定义content属性
public
void setName(String name){
this.name = name ;
}
public void
setContent(String content){
this.content = content ;
}
public
String getName(){
return this.name ;
}
public String
getContent(){
return this.content ;
}
};
class Producer
implements Runnable{ // 通过Runnable实现多线程
private Info info = null ; //
保存Info引用
public Producer(Info info){
this.info = info
;
}
public void run(){
boolean flag = false ; // 定义标记位
for(int
i=0;i<50;i++){
if(flag){
this.info.setName("李兴华") ; //
设置名称
try{
Thread.sleep(90) ;
}catch(InterruptedException
e){
e.printStackTrace() ;
}
this.info.setContent("JAVA讲师")
; // 设置内容
flag = false ;
}else{
this.info.setName("mldn")
; // 设置名称
try{
Thread.sleep(90)
;
}catch(InterruptedException e){
e.printStackTrace()
;
}
this.info.setContent("http://www.mldnjava.cn/") ; // 设置内容
flag = true
;
}
}
}
};
class Consumer implements Runnable{
private
Info info = null ;
public Consumer(Info info){
this.info = info
;
}
public void run(){
for(int
i=0;i<50;i++){
try{
Thread.sleep(90)
;
}catch(InterruptedException e){
e.printStackTrace()
;
}
System.out.println(this.info.getName() +
" --> " +
this.info.getContent()) ;
}
}
};
public class
ThreadCaseDemo01{
public static void main(String args[]){
Info info =
new Info(); // 实例化Info对象
Producer pro = new Producer(info) ; //
生产者
Consumer con = new Consumer(info) ; // 消费者
new Thread(pro).start()
;
new Thread(con).start() ;
}
};
13 解决方法,等待与唤醒
class
Info{ // 定义信息类
private String name = "李兴华"; // 定义name属性
private String
content = "JAVA讲师" ; // 定义content属性
private boolean flag = false ; //
设置标志位
public synchronized void set(String name,String
content){
if(!flag){
try{
super.wait()
;
}catch(InterruptedException e){
e.printStackTrace()
;
}
}
this.setName(name) ; //
设置名称
try{
Thread.sleep(300) ;
}catch(InterruptedException
e){
e.printStackTrace() ;
}
this.setContent(content) ; //
设置内容
flag = false ; // 改变标志位,表示可以取走
super.notify() ;
}
public
synchronized void get(){
if(flag){
try{
super.wait()
;
}catch(InterruptedException e){
e.printStackTrace()
;
}
}
try{
Thread.sleep(300)
;
}catch(InterruptedException e){
e.printStackTrace()
;
}
System.out.println(this.getName() +
" --> " +
this.getContent()) ;
flag = true ; // 改变标志位,表示可以生产
super.notify()
;
}
public void setName(String name){
this.name = name
;
}
public void setContent(String content){
this.content = content
;
}
public String getName(){
return this.name ;
}
public
String getContent(){
return this.content ;
}
};
class Producer
implements Runnable{ // 通过Runnable实现多线程
private Info info = null ; //
保存Info引用
public Producer(Info info){
this.info = info
;
}
public void run(){
boolean flag = false ; // 定义标记位
for(int
i=0;i<50;i++){
if(flag){
this.info.set("李兴华","JAVA讲师") ; //
设置名称
flag = false ;
}else{
this.info.set("mldn","http://www.mldnjava.cn/") ; //
设置名称
flag = true ;
}
}
}
};
class Consumer
implements Runnable{
private Info info = null ;
public Consumer(Info
info){
this.info = info ;
}
public void run(){
for(int
i=0;i<50;i++){
this.info.get() ;
}
}
};
public class
ThreadCaseDemo03{
public static void main(String args[]){
Info info =
new Info(); // 实例化Info对象
Producer pro = new Producer(info) ; //
生产者
Consumer con = new Consumer(info) ; // 消费者
new Thread(pro).start()
;
new Thread(con).start() ;
}
};