Java Experiment 7-multithreaded programming __ Programming

Source: Internet
Author: User

1 Creating threads using thread and runnable

"Experimental Purposes"

(1) To understand the use of the implementation of runnable interface methods to achieve multithreading.

(2) Master the setting of thread priority.

(3) Deepen the understanding of the thread state transition.

"Experimental Requirements"

Request one: The use of multithreading to achieve the rotation of the planet

Requirement two: Write a thread Syncdemo, design a method public class GetNumber () count from 1 to 20 stop. Among them, design two synchronization threads Athread and Bthread, all use GetNumber () to scramble these 20 numbers. Note the synchronous control of the thread athread and bthread to ensure that the count is stopped by 20.

Class Syncdemo implements Runnable {
	static int num = 1;
	Public synchronized Boolean GetNumber () throws Exception {
		thread.sleep (MB);
		if (num <=) {
			System.out.println (Thread.CurrentThread (). GetName () + "" +num++);
			return true;
		}
		return false;
	}

	public void Run () {a
		try {while
			(GetNumber ());
		} catch (Exception e) {
			//TODO auto-generated catch Blo CK
			e.printstacktrace ();
}} public class Fd {

	/**
	 * @param args */public
	static void Main (string[] args) {
		//TODO Auto-gene Rated method Stub
		Syncdemo demo = new Syncdemo ();
		Thread athread = new Thread (demo, "A");
		Thread bthread = new Thread (demo, "B");
		Athread.start ();
		Bthread.start ();
	}


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.