1、线程的概念
/**
* 现代操作系统都是多任务系统
* 所谓多任务操作系统即:一台计算机可以同时运行多个程序,而一个程序一般我们将其认为是一个"进程"
* 线程是比进程跟小的运行单元
* 如果我们希望一个程序在运行中能够同时处理多个事务,我们就需要使用多线程
*
* 进程的概念:
*
*
*
*/
public class Test1 {
public static void main(String[] args) {
String str="OKOK";
System.out.println(str);
}
}
2、创建线程
class TTest1 extends Thread{
public void run() {
for(int i=0;i<10;i++){
System.out.println("■"+i);
try{
this.sleep(10);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
/**
* java中实现线程的方式[两种]
* 2、实现Runnable接口
*
* 思考:到底使用方式1合适 还是使用方式2恰当
* 方式2更能体现面向对象编程的设计[因为java是单根继承体系,如果继承了Thread类则无法进行其他类的拓展]
*/
public class Test3 {
public static void main(String[] args) {
Thread t1=new Thread(new ThreadTest1());
Thread t2=new Thread(new ThreadTest2());
t1.start();
t2.start();
}
}
class ThreadTest1 implements Runnable{
public void run() {
for(int i=1;i<=5;i++){
System.out.print("★ ");
try{
Thread.sleep(300);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
3、线程调度
public class Test1 {
public static void main(String[] args) {
Thread t1=new MyThread1();
Thread t2=new MyThread2();
Thread t3=new MyThread3();
t1.setPriority(5);
t2.setPriority(5);
t3.setPriority(6);
System.out.println(t1.getPriority());
System.out.println(t2.getPriority());
System.out.println(t3.getPriority());
t1.start();
t2.start();
t3.start();
}
}
class MyThread1 extends Thread{
public void run(){
for(int i=1;i<11;i++){
System.out.println("■"+i);
try{
this.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
class MyThread2 extends Thread{
public void run(){
for(int i=1;i<11;i++){
System.out.println("◆"+i);
try{
this.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
class MyThread3 extends Thread{
public void run(){
for(int i=1;i<11;i++){
System.out.println("★"+i);
try{
this.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
public class Test1 {
public static void main(String[] args) {
MyThread1 t1=new MyThread1();
MyThread2 t2=new MyThread2();
MyThread3 t3=new MyThread3();
t3.mt2=t2;
t1.start();
t2.start();
t3.start();
}
}
class MyThread1 extends Thread{
public void run(){
for(int i=1;i<10000;i++){
System.out.println("■"+i);
}
}
}
class MyThread2 extends Thread{
public void run(){
for(int i=1;i<10000;i++){
System.out.println("◆"+i);
}
}
}
class MyThread3 extends Thread{
MyThread2 mt2;
public void run(){
for(int i=1;i<10000;i++){
System.out.println("★"+i);
try{
if(i==4){
mt2.join();
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
public class Test1 {
public static void main(String[] args) {
MyThread1 t1=new MyThread1();
MyThread2 t2=new MyThread2();
MyThread3 t3=new MyThread3();
t1.start();
t2.start();
t3.start();
}
}
class MyThread1 extends Thread{
public void run(){
for(int i=1;i<=10;i++){
System.out.println("■"+i);
try{
this.sleep(5);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
class MyThread2 extends Thread{
public void run(){
for(int i=1;i<=10;i++){
System.out.println("◆"+i);
this.yield();
try{
this.sleep(5);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
class MyThread3 extends Thread{
public void run(){
for(int i=1;i<=10;i++){
System.out.println("★"+i);
try{
this.sleep(5);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}