您现在的位置是:主页 > news > 中国怎么进fiverr网站做任务/淘宝seo关键词的获取方法有哪些
中国怎么进fiverr网站做任务/淘宝seo关键词的获取方法有哪些
admin2025/5/25 11:13:47【news】
简介中国怎么进fiverr网站做任务,淘宝seo关键词的获取方法有哪些,看英语做游戏的网站,获取网站访客qq号线程和进程 进程就是正在运行的程序,是系统进行资源分配和调用的独立单位。 每一个进程都有它自己的内存空间和系统资源。 在一个进程内部又可以执行多个任务,而这每一个任务我们就可以看成是一个线程。是程序使用CPU的基本单位。 多线程有什么意义呢…
线程和进程
进程就是正在运行的程序,是系统进行资源分配和调用的独立单位。
每一个进程都有它自己的内存空间和系统资源。
在一个进程内部又可以执行多个任务,而这每一个任务我们就可以看成是一个线程。是程序使用CPU的基本单位。
多线程有什么意义呢?
多线程的作用不是提高执行速度,而是为了提高应用程序的使用率。
并行和并发
并发 : 指应用能够交替执行不同的任务, 其实并发有点类似于多线程的原理, 多线程并非是如果你开两个线程同时执行多个任务, 执行, 就是在你几乎不可能察觉到的速度不断去切换这两个任务, 已达到"同时执行效果", 其实并不是的, 只是计算机的速度太快, 我们无法察觉到而已. 就类似于你, 吃一口饭喝一口水, 以正常速度来看, 完全能够看的出来, 当你把这个过程以n倍速度执行时…可以想象一下.
并行 : 指应用能够同时执行不同的任务, 例:吃饭的时候可以边吃饭边打电话, 这两件事情可以同时执行
多线程程序实现
1、继承Thread类
public class HelloThread extends Thread {/*** 1、继承Thread类* 2、c重写run()方法* 3、实例化类* 4、调用start()方法*//*** 重写run()方法*/@Overridepublic void run() {// super.run();for (int i = 0; i < 10; i++) {System.out.println("我是子线程:" + i);}}/*** 主方法/主线程* @param args*/public static void main(String[] args) {// 实例化子线程// (new HelloThread()).start();// new HelloThread().start();HelloThread helloThread = new HelloThread();helloThread.start();for (int i = 0; i < 10; i++) {System.out.println("我是主线程main:"+i);}}
}
Thread类的基本获取和设置方法
public final String getName()//获取线程名称
public final void setName(String name)//设置线程名称
其实通过构造方法也可以给线程起名字
public class HelloThreadName extends Thread {public HelloThreadName(String name) {super(name);}@Overridepublic void run() {// super.run();for (int i = 0; i < 10; i++) {System.out.println("子线程名称:" + Thread.currentThread().getName() + ":" + i);}}public static void main(String[] args) {// 实例化子线程// (new HelloThread()).start();// new HelloThread().start();HelloThreadName helloThread = new HelloThreadName("我是子线程1");helloThread.start();Thread.currentThread().setName("我是主线程main1");for (int i = 0; i < 10; i++) {System.out.println("主线程名称:" + Thread.currentThread().getName() + ":"+i);}}
}
public static Thread currentThread()//获取当前执行的线程
public class HelloThreadName extends Thread {@Overridepublic void run() {// super.run();for (int i = 0; i < 10; i++) {System.out.println("子线程名称:" + Thread.currentThread().getName() + ":" + i);}}public static void main(String[] args) {// 实例化子线程// (new HelloThread()).start();// new HelloThread().start();HelloThreadName helloThreadName = new HelloThreadName();helloThreadName.setName("我是子线程1");helloThreadName.start();Thread.currentThread().setName("我是主线程main1");for (int i = 0; i < 10; i++) {System.out.println("主线程名称:" + Thread.currentThread().getName() + ":"+i);}}
}
2、实现Runnable接口 这种方式扩展性强 实现一个接口 还可以再去继承其他类
public class HelloRunnable implements Runnable {// 1、实现Runnable接口// 2、重新run()方法// 3、实例化对象// 4、调用start()方法@Overridepublic void run() {for (int i = 0; i < 10; i++) {System.out.println("我是子线程:" + i);}}/*** 主方法/主线程* @param args*/public static void main(String[] args) {// 实例化子线程// (new Thread(new HelloRunnable())).start();// new Thread(new HelloRunnable()).start();HelloRunnable helloRunnable = new HelloRunnable();Thread thread = new Thread(helloRunnable);thread.start();for (int i = 0; i < 10; i++) {System.out.println("我是主线程main:" + i);}}
}
设置线程名称
public class HelloRunnableName implements Runnable {@Overridepublic void run() {for (int i = 0; i < 10; i++) {System.out.println("子线程的名称:" + Thread.currentThread().getName() + ":" + i);}}public static void main(String[] args) {// 实例化子线程// (new Thread(new HelloRunnable())).start();// new Thread(new HelloRunnable()).start();HelloRunnableName helloRunnableName = new HelloRunnableName();Thread thread = new Thread(helloRunnableName, "我是子线程1");thread.start();Thread.currentThread().setName("我是主线程main1");for (int i = 0; i < 10; i++) {System.out.println("主线程名称:" + Thread.currentThread().getName()+ ":" + i);}}
}
线程优先级
public final int getPriority() //获取线程的优先级
public final void setPriority(int newPriority)//设置线程的优先级
public class ThreadPriority implements Runnable {@Overridepublic void run() {for (int i = 0; i < 10; i++) {System.out.println("线程名称:"+ Thread.currentThread().getName() + ",线程优先级:"+ Thread.currentThread().getPriority() + "循环次数:" + i);}}public static void main(String[] args) {ThreadPriority threadPriority = new ThreadPriority();Thread thread1 = new Thread(threadPriority, "子线程1");thread1.setPriority(Thread.MIN_PRIORITY);thread1.start();Thread thread2 = new Thread(threadPriority, "子线程2");thread2.setPriority(Thread.MAX_PRIORITY);thread2.start();}
}
线程控制之休眠线程
public static void sleep(long millis) 线程休眠
public class ThreadSleep implements Runnable {@Overridepublic void run() {String info[] = {"春眠不觉晓","处处闻啼鸟","夜来风雨声","花落知多少"};for (int i = 0; i < info.length; i++) {System.out.println(info[i]);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}public static void main(String[] args) {ThreadSleep threadSleep = new ThreadSleep();Thread thread = new Thread(threadSleep);thread.start();}
}
线程控制之加入线程
public final void join()
意思就是: 等待该线程执行完毕了以后,其他线程才能再次执行
注意事项: 在线程启动之后,在调用方法
public class ThreadJoin implements Runnable {@Overridepublic void run() {for (int i = 0; i < 10; i++) {System.out.println("线程名称:" + Thread.currentThread().getName() + ":" + i);}}
/*** 主线程* @param args*/public static void main(String[] args) {// 子线程实例化ThreadJoin threadJoin = new ThreadJoin();Thread thread = new Thread(threadJoin, "子线程1");thread.start();Thread.currentThread().setName("主线程main1");for (int i = 0; i < 100; i++) {if (i == 50) {try {thread.join();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("主线程名称:" + Thread.currentThread().getName() + ":"+i);}}
}
线程控制之礼让线程
public static void yield(): 暂停当前正在执行的线程对象,并执行其他线程。
public class ThreadYield implements Runnable {@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + "开始了");Thread.yield();System.out.println(Thread.currentThread().getName() + "结束了");}public static void main(String[] args) {ThreadYield threadYield = new ThreadYield();Thread thread1 = new Thread(threadYield, "线程1");Thread thread2 = new Thread(threadYield, "线程2");thread1.start();thread2.start();}
}
线程控制之守护线程: public final void setDaemon(boolean on):
将该线程标记为守护线程或用户线程。当正在运行的线程都是守护线程时,Java 虚拟机退出。
该方法必须在启动线程前调用。
public class ThreadDaemon {public static void main(String[] args) {// 实例化守护线程Daemon daemon = new Daemon();Thread thread1 = new Thread(daemon);thread1.setDaemon(true);thread1.start();// 实例化用户线程User user = new User();Thread thread2 = new Thread(user);thread2.start();}
}/*** 用户线程*/
class User implements Runnable {@Overridepublic void run() {for (int i = 0; i < 100; i++) {System.out.println(Thread.currentThread().getName() + ":我在努力奔跑:" + i);}}
}/*** 守护线程*/
class Daemon implements Runnable {@Overridepublic void run() {int i = 0;while (true) {System.out.println(Thread.currentThread().getName() + ":我在守护你:" + i++);}}
}
线程控制之中断线程
中断线程
public final void stop(): 停止线程的运行
public void interrupt(): 中断线程(这个翻译不太好),查看API可得当线程调用wait(),sleep(long time)方法的时候处于阻塞状态,可以通过这个方法清除阻塞
public class ThreadStop implements Runnable {private static boolean flag = true;@Overridepublic void run() {int i = 0;while (flag) {System.out.println("我是子线程" + i++);}}/*** 主方法/主线程* @param args*/public static void main(String[] args) {ThreadStop threadStop = new ThreadStop();Thread thread = new Thread(threadStop);thread.start();for (int i = 0; i < 100; i++) {System.out.println("我是主线程" + i);if (i==50) {ThreadStop.flag = false;}}}
}
3、实现 Callable 接口。 相较于实现 Runnable 接口的方式,方法可以有返回值,并且可以抛出异常。
执行 Callable 方式,需要 FutureTask 实现类的支持,用于接收运算结果。 FutureTask 是 Future 接口的实现类
实现步骤
1.创建一个类实现Callable 接口
2.创建一个FutureTask类将Callable接口的子类对象作为参数传进去
3.创建Thread类,将FutureTask对象作为参数传进去
4.开启线程
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;public class HelloCallable implements Callable {@Overridepublic Object call() throws Exception {int j=0;for (int i = 0; i < 10; i++) {System.out.println("我是子线程:" + i);}return j;}public static void main(String[] args) {HelloCallable helloCallable = new HelloCallable();FutureTask futureTask = new FutureTask<>(helloCallable);Thread thread = new Thread(futureTask);thread.start();try {// 获取返回值System.out.println("返回值:" + futureTask.get());} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}
}
}
线程冲突
同步代码块的方式解决线程安全问题
同步代码块的格式
格式:
synchronized(对象){
需要同步的代码;
}
public class SynchronizedStatements implements Runnable {/*** 问题1:输出语句顺序不对* 问题2:出现负值*/// 票数100张private static int ticket = 100;@Overridepublic void run() {while (ticket > 0) {try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}synchronized ("") {if (ticket <= 0)return;System.out.println(Thread.currentThread().getName() + "卖了1张票,还剩" + --ticket + "张");}}}
/*** 主方法** @param args*/public static void main(String[] args) {// 实例化子线程SynchronizedStatements threadConflict = new SynchronizedStatements();// 模拟三个售票员Thread thread1 = new Thread(threadConflict, "售票员1");Thread thread2 = new Thread(threadConflict, "售票员2");Thread thread3 = new Thread(threadConflict, "售票员3");thread1.start();thread2.start();thread3.start();}
}
同步方法解决线程安全问题
public class SynchronizedMethods implements Runnable {/*** 问题1:输出语句顺序不对* 问题2:出现负值*/// 票数100张private static int ticket = 100;
@Overridepublic void run() {while (ticket > 0) {try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}sellTicket();}}/*** 卖票的同步方法*/public synchronized void sellTicket() {if (ticket <= 0)return;System.out.println(Thread.currentThread().getName() + "卖了1张票,还剩" + --ticket + "张");}/*** 主方法* @param args*/public static void main(String[] args) {// 实例化子线程SynchronizedMethods threadConflict = new SynchronizedMethods();// 模拟三个售票员Thread thread1 = new Thread(threadConflict, "售票员1");Thread thread2 = new Thread(threadConflict, "售票员2");Thread thread3 = new Thread(threadConflict, "售票员3");thread1.start();thread2.start();thread3.start();}
}
同步代码块的锁对象: 任意一个对象
同步方法的锁对象: 是this
静态同步方法的锁对象:就是当前类对应的字节码文件对象
线程死锁
如果出现了同步嵌套,就容易产生死锁问题
是指两个或者两个以上的线程在执行的过程中,因争夺资源产生的一种互相等待现象
public class ThreadDeadLock {public static void main(String[] args) {// 实例化线程1DeadLockA deadLockA = new DeadLockA();Thread threadA = new Thread(deadLockA, "线程1");// 实例化线程2DeadLockB deadLockB = new DeadLockB();Thread threadB = new Thread(deadLockB, "线程2");threadA.start();threadB.start();}
}/*** 线程A*/
class DeadLockA implements Runnable {@Overridepublic void run() {synchronized ("A") {System.out.println("线程1获得了A锁");synchronized ("B") {System.out.println("线程1获得了A锁和B锁");}}}
}/*** 线程B*/
class DeadLockB implements Runnable {@Overridepublic void run() {synchronized ("B") {System.out.println("线程2获得了B锁");synchronized ("A") {System.out.println("线程2获得了B锁和A锁");}}}
}
线程协调:解决线程死锁问题
void wait () 在其他线程调用此对象的 notify () 方法或 notifyAll () 方法前,导致当前线程等待。
void wait (long timeout) 在其他线程调用此对象的 notify () 方法或 notifyAll () 方法,或者超过指定的时间量前,导致当前线程等待。
void notify () 唤醒在此对象监视器上等待的单个线程。
void notifyAll () 唤醒在此对象监视器上等待的所有线程。
public class ThreadCoordinate {public static void main(String[] args) {// 实例化线程1DeadLockA deadLockA = new DeadLockA();Thread threadA = new Thread(deadLockA, "线程1");// 实例化线程2DeadLockB deadLockB = new DeadLockB();Thread threadB = new Thread(deadLockB, "线程2");threadA.start();threadB.start();}
}/*** 线程A*/
class DeadLockA implements Runnable {@Overridepublic void run() {synchronized ("A") {System.out.println("线程1获得了A锁");try {"A".wait();} catch (InterruptedException e) {e.printStackTrace();}synchronized ("B") {System.out.println("线程1获得了A锁和B锁");}}}
}/*** 线程B*/
class DeadLockB implements Runnable {@Overridepublic void run() {synchronized ("B") {System.out.println("线程2获得了B锁");synchronized ("A") {System.out.println("线程2获得了B锁和A锁");"A".notifyAll();}}}
}
线程池
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ThreadPool {public static void main(String[] args) {// 实例化服务ExecutorService executorService = Executors.newFixedThreadPool(10);HelloRunnable helloRunnable = new HelloRunnable();// 开启线程executorService.execute(helloRunnable);executorService.execute(helloRunnable);executorService.execute(helloRunnable);// 关闭服务executorService.shutdown();}
}class HelloRunnable implements Runnable {@Overridepublic void run() {System.out.println(Thread.currentThread().getName());}
}
单例模式之懒汉式
public class ThreadSingleton {public static void main(String[] args) {for (int i = 0; i < 100; i++) {HelloRunnable helloRunnable = new HelloRunnable();Thread thread = new Thread(helloRunnable, "线程名称:" + i);thread.start();}}
}/*** 单例*/
class Lazybones {public Lazybones() {System.out.println("对象实例化了");}private static Lazybones lazybones = null;public static Lazybones getLazybones() {synchronized ("") {if (lazybones == null) {lazybones = new Lazybones();}}return lazybones;}
}/*** 多线程*/
class HelloRunnable implements Runnable {@Overridepublic void run() {Lazybones.getLazybones();}
}