Black Horse Programmer----Multi-threaded Foundation

Source: Internet
Author: User

/**
* @author Administrator
*
* @description Java Multithreaded Programming Primer test Class
* @history
*/
Method one, inheriting thread class thread
Class MyThread extends thread{
Public MyThread (String threadname) {//sets the name of the current thread
CurrentThread (). SetName (ThreadName);
}
public void Run () {
System.out.println (Thread.CurrentThread (). GetName ());
}
}
Method Two, realize runnable interface
Class MyThread2 implements runnable{
public void Run () {

System.out.println (Thread.CurrentThread (). GetName ());
}
}
public class Syntestdemo {
/**
* @description
* @param args
*/
public static void main ( String[] (args) {
//1, thread definition, thread and process differences, why threading is introduced, etc.
//2. There are two main ways to implement multithreading in Java: Inheriting the thread class and implementing the Runnable interface
//3, Synchronous processing is required when multiple threads concurrently access common critical resources, and excessive synchronization can cause deadlock
problems
MyThread t1 = new MyThread ("Hello-thread");
T1.start (); Start thread 1
MyThread2 t2 = new MyThread2 ();
New Thread (T2). Start (); Start thread 2
}
}
/**
* @author Administrator
*
* @description Multithreaded Programming Walkthrough Example
* @history
*/
Public Class Synabctest {
/**
* @description
* @param args
*/
public static void Main (string[] args) {
//through specific Example to deepen the understanding of multithreading
//The problem is: Cycle printing 10 times abcabc ... ABC
Printthread p1 = new Printthread (0, "A");//parameters are thread flags and printed content
Printthread p2 = new Printthread (1, "B");
Printthread p3 = new Printthread (2, "C");
//Start thread a B C
new Thread (p1). Start ();
New Thread (P2). Start ();
New Thread (P3). Start ();
}

}
Defining threading classes in a way that implements interfaces
Class Printthread implements Runnable {
Tag execution currently should execute thread 0, 1, and 2 in turn represents a B C
defined as static variables, because each thread uses a separate stack
private static int index = 0;
private static Object lock = new Object ();
private int key = 0; Thread Flags
private int print = 0; Number of Prints
private String name; What to print
public printthread (int key, String name) {
This.key = key;
THIS.name = name;
}
public void Run () {
while (This.print < 10) {//print number of times
Synchronized (lock) {
while (!) ( This.key = = index% 3) {//start from 0
try {
Lock.wait (); Block off
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
System.out.print (this.name); Print out content
this.print++; Current thread print count + +
index++; Thread Switch Next
Lock.notifyall (); Wake up the other waiting threads
}
}
}
}
/**
* @author Administrator
*
* @description Deadlock Simulation test class
* @history
*/
public class Deadlocktest {
/**
* @description
* @param args*/
public static void Main (string[] args) {
Too many synchronization operations can cause deadlock problems, and deadlocks are caused by loops waiting
By simulating two thread objects, thread A requires resources 1 and 2 for a single operation, and thread B is the same
In the process of resource allocation, thread A takes up resource 1, waits for resource 2, at this point, thread B takes up resource 2, waits for resource 1
Deadthread dt1 = new Deadthread ("1", true);
Deadthread DT2 = new Deadthread ("2", false);
New Thread (DT1). Start (); Start thread 1
New Thread (DT2). Start (); Start Thread 2
}
Defines a static inner class, similar to an external class.
Static class Deadthread implements runnable{
/**
* Define resources 1 and Resources 2 Lock1 and Lock2
*/
private static Object lock1 = new Object ();
private static Object Lock2 = new Object ();
private String name; Thread Name
Private Boolean run; Execution Order Flag
Public Deadthread (String Name,boolean run) {
THIS.name = name;
This.run = run;
}
@Override
public void Run () {
if (This.run) {
Thread 1 consumes Resources first 1
Synchronized (LOCK1) {
try {
System.out.println ("Thread1 used Lock1");
Thread.Sleep (3000); Temporarily sleeps for 3 seconds
} catch (Interruptedexception e) {
E.printstacktrace ();
}
Thread 1 again to apply for resource 2, at this time resource 2 has been occupied by thread 2 is not released
Synchronized (LOCK2) {
System.out.println ("Hello Dead-lock");
}
}
}else{
Thread 2 consumes Resources first 2
Synchronized (LOCK2) {
try {
System.out.println ("Thread2 used Lock2");
Thread.Sleep (3000); Thread 2 temporarily sleeps for 3 seconds
} catch (Interruptedexception e) {
E.printstacktrace ();
}
Thread 2 again to apply for resource 1, at this time resource 1 has been occupied by thread 1 is not released
Synchronized (LOCK1) {
System.out.println ("Hello Dead-lock");
}
}
}
}
}
}
Class MyThread1 implements Runnable {
Private Boolean flag = true; Defining Flag bits
public void Run () {
int i = 0;
while (This.flag) {
System.out.println (Thread.CurrentThread (). GetName () + "run, I ="
+ (i++));
}
}
public void Stop () {
This.flag = false; Modify Flag bit
}
}
/**
* @author Administrator
*
* @description Stop threads by modifying marker bits
* @history
*/
public class Threadstopdemo {
public static void Main (string[] args) {
MyThread1 my = new MyThread1 ();
Thread t = new Thread (My, "thread"); To create a thread object
T.start (); Start thread
try {
Thread.Sleep (50); Due to delay under
} catch (Exception e) {
E.printstacktrace ();
}

My.stop ();//modify flag bit, stop running

}

}

Black Horse Programmer----Multi-threaded Foundation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.