简单线程的实现

一,开源免费的框架
struct
mabtis
spring


二, java_thread  网络线程
  什么是线程?
  什么是进程?
  进程:一个进程对应一个应用程序,它是系统中正在运行的应用程序,它拥有一个独立的内存空间
  window:单用户多进程
  dos:单用户单进程
  lunix:多用户多线程
  线程:运行在操作系统中之上的每个应用程序,都会占用一个独立的进程,而进程内又允许运行多个线程,这就意味着一个程序可以运行多个任务的功能。
线程是模拟进程
线程是指进程中一个执行流程,一个进程中允许多个线程,他们分别执行不同的任务
  单线程:从上往下执行
  匿名内部类:存放在方法的内部,只能创建一个对象
  案例:
  public class Thread01 {
public static void main(String[] args) {
new Thread(){
public void run(){
for(int i='a';i<'z';i++){
System.out.println(Thread.currentThread().getName()+"==>"+(char)i);
}
}
}.start();
for(int i=0;i<=100;i++){
System.out.println(Thread.currentThread().getName()+"==>"+i);
}
}
}


在这里还要一点要注意:类名不能为Thread 多线程可以高效利用CPU
案例:创建一个线程
1.并没有使用线程
public class Thread02 {
public static void main(String[] args) {
MyThread mt=new MyThread();
mt.run();
MyThread mt1=new MyThread();
mt1.run();
}


}
//创建一个线程
//1.继承Thread类
class MyThread extends Thread{
//2.重写run方法
public void run(){//执行的内容
for(int i='a';i<'z';i++){
System.out.println(Thread.currentThread().getName()+"==>"+(char)i);
}
}
}
使用一个run方法只是调用了run方法
==========================================
2.使用了线程:第一种方式:继承Thread
public class Thread02 {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+"这是一个主线程");
//3.创建线程对象
MyThread mt=new MyThread();
mt.setName("线程A");
//4.启动线程
mt.start();
MyThread mt1=new MyThread();
mt1.setName("线程B");
mt1.start();
}
}
//创建一个线程
//1.继承Thread类
class MyThread extends Thread{
//2.重写run方法
public void run(){//执行的内容
for(int i='a';i<'z';i++){
System.out.println(Thread.currentThread().getName()+"==>"+(char)i);
}
}
}
========================================
第二种方式 :实现Runnable
public class Thread03 {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+"这是一个主线程");
//3.通过Thread类创建线程对象
MyThread01 mt=new MyThread01();
Thread t1=new Thread(mt,"线程A");
//4.启动线程
t1.start();
MyThread01 mt1=new MyThread01();
Thread t2=new Thread(mt1,"线程B");
t2.start();
}


}
//创建一个线程
//1.实现runnable接口
class MyThread01 implements Runnable{
//2.重写run方法
public void run(){//执行的内容
for(int i='a';i<'z';i++){

System.out.println(Thread.currentThread().getName()+"==>"+(char)i);
}
}
}
==================================================================
实现一个简单的时钟:隔一秒跳一下  使用线程接口实现
public class Time {
public static boolean FLAG=true;
public static void main(String[] agrs){
Scanner input=new Scanner(System.in);
//new Thread(new MyThread02()).start();
MyThread02 my=new MyThread02();
Thread t1=new Thread(my,"时钟在跳动");
t1.start();
while(true){
System.out.print("是否"+(FLAG ? "是否停止时钟":"是否启动时钟"));
String answer=input.next();
if("y".equalsIgnoreCase(answer)){
if(FLAG){
FLAG=false;
}{
FLAG=true;
t1.start();
}
}
}
}


}
class MyThread02 implements Runnable{
//2.重写run方法
public void run(){//执行的内容
while(Time.FLAG){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
System.out.println(sdf.format(new Date()));
}
}
}
===================================================================
实现一个简单的时钟:隔一秒跳一下  使用线程类实现
public class Time1 {
public static boolean FLAG=true;
public static void main(String[] agrs){
Scanner input=new Scanner(System.in);
MyThread03 my=new MyThread03();

my.start();
while(true){
System.out.print("是否"+(FLAG ? "是否停止时钟":"是否启动时钟"));
String answer=input.next();
if("y".equalsIgnoreCase(answer)){
if(FLAG){
FLAG=false;
}{
FLAG=true;
my.start();
}
}
}
}


}
class MyThread03 extends Thread{
//2.重写run方法
public void run(){//执行的内容
while(Time1.FLAG){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
System.out.println(sdf.format(new Date()));
}
}
}
======================================================================
//写一个泡茶的程序
1.烧水10000
2.洗杯子5个1500
3.水烧好了,杯子洗好了,开始沏茶
public class Tea {
public static void main(String[] args) {
Shao s=new Shao();
s.start();
Xi x=new Xi();
x.start();
}
}
//烧水
class Shao extends Thread{
public void run(){
System.out.println("开始烧水");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("水烧好了,开始泡茶。。。");
}

}
//洗杯子
class Xi extends Thread{
public void run(){
for(int i=1;i<=5;i++){
  System.out.println("开始洗杯子:"+i+"个杯子");
  try {
  Thread.sleep(1500);
  } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  System.out.println("第"+i+"个杯子洗好了。。。");
}
}

}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wen's

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值