多线程(两种方式实现)
一.继承Thread
class Threa extends Thread {
@Override
public void run() {//线程方法
ss();
}
public static void ss() {
for (int x = 0; x < 100; x++) {
System.out.println(x);
}
}
}
二.实现Runnable(推荐)
class Th implements Runnable {
@Override
public void run() {//线程方法
bb();
}
public static synchronized void bb() {//同步方法
for (int x = 0; x < 10; x++) {
System.out.println(x);
}
}
}
多线程与同步(同步是为了不发生资源混乱的,一种资源保护政策(多线程启动时会抢占资源))
public class Test {
public static void main(String[] args) {
Threa threa = new Threa();
Threa threa2 = new Threa();
threa.start();//启动线程
threa2.start();
new Thread(new Th()).start();//启动线程
new Thread(new Th()).start();
}
}
class Threa extends Thread {
@Override
public void run() {
ss();//线程方法
}
public static void ss() {
for (int x = 0; x < 100; x++) {
System.out.println(Thread.currentThread().getName() + ":" + x);//输出线程名和变量
}
}
}
class Th implements Runnable {
@Override
public void run() {
bb();
}
public static synchronized void bb() {//同步方法
for (int x = 0; x < 10; x++) {
System.out.println(Thread.currentThread().getName() + ":" + x);//输出线程名和变量
}
}
}
多线程另类实现
import java.util.concurrent.FutureTask;
public class Threadw {
FutureTask ft = new FutureTask<>(() -> {
for (int i = 1; i < 101; i++) {
System.out.println("one:"+i);
}
return 1;
});//jdk8新特性(函数表达式)
FutureTask<Integer> ft2 = new FutureTask<>(() -> {
for (int i = 1; i < 101; i++) {
System.out.println("two:"+i);
}
return 1;
});//jdk8新特性(函数表达式)
public static void main(String[] args) {
Threadw threadw = new Threadw();
new Thread(threadw.ft).start();//启动线程
new Thread(threadw.ft2).start();
}
}
Thread类的getName方法和getID方法
public class Three extends Thread {
@Override
public void run() {
System.out.println("java");
}
public static void main(String[] args) {
/*
* 3、测试Thread类的getName方法和getID方法, (1)、创建两个线程,输出默认的线程名字和默认的ID。
* (2)、创建一个线程,设置线程的名字并输出线程名字和默认的ID。
*/
Three th = new Three();
Three th2 = new Three();
Three th3 = new Three();
System.out.println(th.getId() + " " + th.getName());
System.out.println(th2.getId() + " " + th2.getName());
th3.setName("java");//设置线程的名字
System.out.println(th3.getId() + " " + th3.getName());
}
}
守护线程
public class Four extends Thread {
public static void main(String[] args) {
/*
* 4、守护线程 (1)、使用内部类创建线程的方式创建线程d,该线程实现每隔0.1秒输出字符串“后台线程”。
* (2)、设置线程d为守护线程并启动该线程。 (3)、使用main线程阻塞5秒,然后输出字符串“main”线程结束了。
*/
try {
TestThread tt = new TestThread();
tt.setDaemon(true);//设置线程tt为守护线程
tt.start();//启动该线程
currentThread().sleep(5000);//main线程阻塞5秒
System.out.println("main");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
}
class TestThread extends Thread{
@Override
public void run() {
while(true){
try {
currentThread().sleep(100);//main线程阻塞0.1秒
System.out.println("后台线程");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
复制文件的线程
class Test1 extends Thread {
@Override
public void run() {
try {
File file = new File("eclipse-jee-neon-3-win32-x86_64.zip");
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream("eclipse-jee-winx_64.zip");
byte[] b = new byte[1024];
int x = 0;
int w = 0;
int s = 0;
while ((fis.read(b)) != -1) {
fos.write(b);
w++;
if(w==file.length()/1024/100){
s++;
System.out.println("正在复制文件:"+s+"%");
w=0;
}
}
System.out.println("复制完成!!!");
System.out.println("文件名称:" + file.getName());
System.out.println("文件大小:" + file.length()+"b");
System.out.println("文件路径:" + file.getAbsolutePath());
System.out.println("最后修改时间:" + file.lastModified());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Test2 extends Thread {
@Override
public void run() {
System.out.println("等待文件复制完毕:");
Test1 test1 = new Test1();
test1.start();
}
}
public class Test1{
int c = 0;
boolean b = false;
public synchronized void terun() {//同步方法
try {
this.wait(100);//等待0.1秒
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (int i = 1; i < 53; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
c++;
if(c == 2){
try {
c = 0;
this.notifyAll();//解除所有等待的线程
this.wait();//线程进入等待
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public synchronized void tarun() {//同步方法
try {
this.notifyAll();//解除所有等待的线程
this.wait();//线程进入等待
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (char i = 'A'; i <= 'Z'; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
c++;
if(c == 1){
c = 0 ;
try {
this.notifyAll();//解除所有等待的线程
this.wait();//线程进入等待
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public class One {
public static void main(String[] args) {
/*
* 1、写2个线程,其中一个线程打印1~52,另一个线程打印A~Z,打印顺序应该是12A34B56C...5152Z。
* 提示,需要利用多线程通信知识。并用两种方式实现。
*/
Test1 test1 = new Test1();
new Thread(new Onew(test1)).start();
new Thread(new Onea(test1)).start();
}
}
class Onea implements Runnable {
private Test1 test1;
Onea(Test1 test1) {
this.test1 = test1;
}
@Override
public void run() {
test1.terun();
}
}
class Onew implements Runnable {
private Test1 test1;
Onew(Test1 test1) {
this.test1 = test1;
}
@Override
public void run() {
test1.tarun();
}
}
线程组
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
public class Three {
/* 3、使用ExecutorService实现线程池。
(1)、线程池要执行的任务为每隔一秒输出一次当前线程的名字,总计输出10次
(2)、创建一个线程池,该线程池中只有两个空线程
(3)、使线程池执行5次步骤一的任务。*/
public static void main(String[] args) {
ExecutorService es = new ScheduledThreadPoolExecutor(1);
es.submit(()->{
for(int i=0;i<10;i++){
try {
Thread.currentThread();
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
ExecutorService es2 = new ScheduledThreadPoolExecutor(2);
es2.submit(()->{
for(int i=0;i<10;i++){
try {
Thread.currentThread();
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
es2.submit(()->{
for(int i=0;i<10;i++){
try {
Thread.currentThread();
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
es2.submit(()->{
for(int i=0;i<10;i++){
try {
Thread.currentThread();
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
es2.submit(()->{
for(int i=0;i<10;i++){
try {
Thread.currentThread();
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
es2.submit(()->{
for(int i=0;i<10;i++){
try {
Thread.currentThread();
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}