2019.10.10

1.继承

代码:
package MyThread;

public class Myextends extends Thread{
    public Myextends(String name) {
        super(name);
    }
    public void run() {
        for(int i=0;i<3;i++) {
            System.out.println(this.getName()+"打印信息"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        Myextends thread1=new Myextends("线程1");
        Myextends thread2=new Myextends("线程2");
        thread1.start();
        thread2.start();
    }
}

运行截图:
2019.10.10_第1张图片

2.实现Runnable方法

代码:
package MyThread;

public class MyRunnable implements Runnable{
    String name;
    public MyRunnable(String name) {
        this.name=name;
    }
    public void run() {
        for(int i=0;i<3;i++) {
            System.out.println(name+"打印信息"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        MyRunnable myrunnable1=new MyRunnable("线程1");
        MyRunnable myrunnable2=new MyRunnable("线程2");
        Thread thread1=new Thread(myrunnable1);
        Thread thread2=new Thread(myrunnable2);
        thread1.start();
        thread2.start();
    }
}

运行截图:
2019.10.10_第2张图片

3.优先级

代码:
package MyThread;

public class Myextends extends Thread{
    public Myextends(String name) {
        super(name);
    }
    public void run() {
        for(int i=0;i<3;i++) {
            System.out.println(this.getName()+"打印信息"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        Myextends thread1=new Myextends("线程1");
        Myextends thread2=new Myextends("线程2");
        thread1.start();
        thread2.start();
    }
}

运行截图;
2019.10.10_第3张图片

4.文字移动

代码:
package MyThread;

import java.awt.Font;

import javax.swing.*;
public class MyThreadRmove {
    JFrame f;
    JPanel p;
    JLabel l;
    public MyThreadRmove() {
        f=new JFrame("文字移动");
        p=new JPanel();
        l=new JLabel("模拟字幕移动测试");
        f.setVisible(true);
        f.setSize(600, 400);
        f.add(p);
        p.setLayout(null);
        p.add(l);
        l.setBounds(0, 140, 200, 20);
        l.setFont(new Font("宋体",Font.BOLD,20));
        f.setBounds(450, 200, 600, 400);
    }
    public void f() {
        Thread t=new Thread(new Runnable(){
            public void run() {
                int i=5,j=420;
                while(true) {
                    if(i<420) {
                        try {
                            Thread.sleep(60);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        l.setBounds(i, 140, 200, 20);
                        i=i+5;
                    }
                    else if(j>=0) {
                        try {
                            Thread.sleep(60);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        l.setBounds(j, 140, 200, 20);
                        j=j-5;
                        if(j==0) {
                            i=5;
                            j=420;
                        }
                    }
                }
            }
        });
        t.start();
    }
    public static void main(String[] args) {
        MyThreadRmove mtr=new MyThreadRmove();
        mtr.f();
    }
}

运行截图:
2019.10.10_第4张图片

2019.10.10_第5张图片

5.时钟

代码:
package MyThread;

import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;

public class MyTime implements Runnable{
    JFrame f;
    JLabel l;
    public MyTime() {
        f=new JFrame("NowTimes");
        l=new JLabel();
        f.setVisible(true);
        f.setSize(300, 300);
        f.setLayout(null);
        f.add(l);
        f.setBounds(600, 300, 350, 200);
        l.setBounds(10, 60, 320, 20);
    }
    public void run() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时:mm分:ss秒");
        Font font = new Font("楷体",Font.BOLD,20);
        l.setFont(font);
        while(true) {
            Date date=new Date();
            l.setText(sdf.format(date));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }   
    }
    public static void main(String[] args) {
        MyTime mt=new MyTime();
        Thread th=new Thread(mt);
        th.start();
    }
}

运行截图:
2019.10.10_第6张图片

你可能感兴趣的:(2019.10.10)