学习java第20天
1.java中的线程
线程:程序中单个顺序的流程控制
2.线程体
run方法来实现
3.创建线程的两种方法
*继承Thread类创建线程
public class TestThread1 {
public static void main(String args[]){
Thread t = new MyThread(100);
t.start();
}
}
public static void main(String args[]){
Thread t = new MyThread(100);
t.start();
}
}
class MyThread extends Thread {
private int n;;
public MyThread( int n ){
super();
this.n=n;
}
public void run() {
for(int i=0;i<n;i++) {
System.out.print (" " + i);
}
}
}
private int n;;
public MyThread( int n ){
super();
this.n=n;
}
public void run() {
for(int i=0;i<n;i++) {
System.out.print (" " + i);
}
}
}
*通过向Thread构造方法传递Runnable对象创建线程
public class TestThread2 {
public static void main(String args[]) {
MyTask mytask = new MyTask(10);
Thread thread = new Thread(mytask);
thread.start();
//thread.setDaemon(true);
public static void main(String args[]) {
MyTask mytask = new MyTask(10);
Thread thread = new Thread(mytask);
thread.start();
//thread.setDaemon(true);
for(int i=0; i<6; i++) {
System.out.println("Main--" + i);
try{
Thread.sleep(500);
}catch( InterruptedException e ){}
}
System.out.println("Main--" + i);
try{
Thread.sleep(500);
}catch( InterruptedException e ){}
}
}
}
}
class MyTask implements Runnable {
private int n;
public MyTask(int n){
this.n = n;
}
public void run() {
for(int i=0; i<n; i++) {
System.out.println(" " + i);
try{
Thread.sleep(500);
}catch( InterruptedException e ){}
}
}
}
private int n;
public MyTask(int n){
this.n = n;
}
public void run() {
for(int i=0; i<n; i++) {
System.out.println(" " + i);
try{
Thread.sleep(500);
}catch( InterruptedException e ){}
}
}
}
4.匿名类和Lambda表达式
可以匿名类来实现Runnable
new Thread{
public void (){
for(int i = 0; i<10; i++)
System.out.println(i);
}
}.star();
也可以用Lambda表达式
public class TestThread4Anonymous {
public static void main(String args[]) {
public static void main(String args[]) {
new Thread(){
public void run() {
for(int i=0; i<10; i++)
System.out.println(i);
}
}.start();
public void run() {
for(int i=0; i<10; i++)
System.out.println(i);
}
}.start();
new Thread( ( ) -> {
for(int i=0; i<10; i++)
System.out.println(" "+ i);
} ).start();
}
}
for(int i=0; i<10; i++)
System.out.println(" "+ i);
} ).start();
}
}
多线程:
import java.util.*;
import java.text.*;
public class TestThread3 {
public static void main(String args[]) {
Counter c1 = new Counter(1);
Thread t1 = new Thread(c1);
Thread t2 = new Thread(c1);
Thread t3 = new Thread(c1);
Counter c2 = new Counter(2);
Thread t4 = new Thread(c2);
Thread t5 = new Thread(c2);
Thread t6 = new Thread(c2);
TimeDisplay timer = new TimeDisplay();
Thread t7 = new Thread(timer);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
t7.start();
}
}
import java.text.*;
public class TestThread3 {
public static void main(String args[]) {
Counter c1 = new Counter(1);
Thread t1 = new Thread(c1);
Thread t2 = new Thread(c1);
Thread t3 = new Thread(c1);
Counter c2 = new Counter(2);
Thread t4 = new Thread(c2);
Thread t5 = new Thread(c2);
Thread t6 = new Thread(c2);
TimeDisplay timer = new TimeDisplay();
Thread t7 = new Thread(timer);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
t7.start();
}
}
class Counter implements Runnable {
int id;
Counter(int id){
this.id = id;
}
public void run() {
int i=0;
while( i++<=10 ){
System.out.println("ID: " + id + " No. " + i);
try{ Thread.sleep(10); } catch( InterruptedException e ){}
}
}
}
int id;
Counter(int id){
this.id = id;
}
public void run() {
int i=0;
while( i++<=10 ){
System.out.println("ID: " + id + " No. " + i);
try{ Thread.sleep(10); } catch( InterruptedException e ){}
}
}
}
class TimeDisplay implements Runnable {
public void run(){
int i=0;
while( i++<=3 ){
System.out.println(
new SimpleDateFormat().format( new Date()));
try{ Thread.sleep(40); } catch( InterruptedException e ){}
}
}
}
public void run(){
int i=0;
while( i++<=3 ){
System.out.println(
new SimpleDateFormat().format( new Date()));
try{ Thread.sleep(40); } catch( InterruptedException e ){}
}
}
}
5.多线程的控制
*基本控制
启动: start()
结束: 设定一个标记变量,来结束方法
暂时阻止: try { Thread.sleep(1000); }catch( InterruptedException e) { }
6.后台线程:
*普通线程
非Daemon线程
*Daemon线程
import java.util.*;
public class TestThreadDaemon {
public static void main(String args[]) {
Thread t = new MyThread();
t.setDaemon(true);
t.start();
public class TestThreadDaemon {
public static void main(String args[]) {
Thread t = new MyThread();
t.setDaemon(true);
t.start();
System.out.println( "Main--" + new Date());
try{ Thread.sleep(500); }
catch(InterruptedException ex){}
System.out.println("Main End");
}
}
try{ Thread.sleep(500); }
catch(InterruptedException ex){}
System.out.println("Main End");
}
}
class MyThread extends Thread {
public void run() {
for(int i=0; i<10; i++ ){
System.out.println( i + "--" + new Date());
try{ Thread.sleep(100); }
catch(InterruptedException ex){}
}
}
}
public void run() {
for(int i=0; i<10; i++ ){
System.out.println( i + "--" + new Date());
try{ Thread.sleep(100); }
catch(InterruptedException ex){}
}
}
}
明天学习内容:
线程的同步