Creating Threads by Extending a Thread Class

2 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€85.10
List Price:  €119.14
You save:  €34.04
£73.85
List Price:  £103.39
You save:  £29.54
CA$136.15
List Price:  CA$190.62
You save:  CA$54.46
A$151.31
List Price:  A$211.84
You save:  A$60.53
S$127.72
List Price:  S$178.82
You save:  S$51.09
HK$784.91
List Price:  HK$1,098.90
You save:  HK$313.99
CHF 79.46
List Price:  CHF 111.25
You save:  CHF 31.78
NOK kr1,010.30
List Price:  NOK kr1,414.46
You save:  NOK kr404.16
DKK kr635.22
List Price:  DKK kr889.34
You save:  DKK kr254.11
NZ$165.45
List Price:  NZ$231.65
You save:  NZ$66.19
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳12,146.16
List Price:  ৳17,005.11
You save:  ৳4,858.95
₹8,641.69
List Price:  ₹12,098.71
You save:  ₹3,457.02
RM421.50
List Price:  RM590.12
You save:  RM168.62
₦152,483.75
List Price:  ₦213,483.35
You save:  ₦60,999.60
₨28,367.60
List Price:  ₨39,715.78
You save:  ₨11,348.17
฿3,226.67
List Price:  ฿4,517.47
You save:  ฿1,290.80
₺4,047.24
List Price:  ₺5,666.30
You save:  ₺1,619.05
B$551.80
List Price:  B$772.54
You save:  B$220.74
R1,762.19
List Price:  R2,467.13
You save:  R704.94
Лв166.26
List Price:  Лв232.77
You save:  Лв66.51
₩137,107.12
List Price:  ₩191,955.46
You save:  ₩54,848.33
₪334.16
List Price:  ₪467.85
You save:  ₪133.68
₱5,664.83
List Price:  ₱7,930.99
You save:  ₱2,266.16
¥14,655.78
List Price:  ¥20,518.68
You save:  ¥5,862.90
MX$1,855.79
List Price:  MX$2,598.18
You save:  MX$742.39
QR363.25
List Price:  QR508.56
You save:  QR145.31
P1,329.84
List Price:  P1,861.83
You save:  P531.98
KSh12,918.70
List Price:  KSh18,086.70
You save:  KSh5,168
E£4,907.77
List Price:  E£6,871.08
You save:  E£1,963.30
ብር13,614.65
List Price:  ብር19,061.06
You save:  ብር5,446.40
Kz91,186.38
List Price:  Kz127,664.58
You save:  Kz36,478.20
CLP$94,846.51
List Price:  CLP$132,788.91
You save:  CLP$37,942.40
CN¥715.66
List Price:  CN¥1,001.96
You save:  CN¥286.29
RD$6,031.44
List Price:  RD$8,444.26
You save:  RD$2,412.81
DA12,943.30
List Price:  DA18,121.14
You save:  DA5,177.84
FJ$223.59
List Price:  FJ$313.04
You save:  FJ$89.44
Q764.76
List Price:  Q1,070.69
You save:  Q305.93
GY$20,835.41
List Price:  GY$29,170.41
You save:  GY$8,334.99
ISK kr12,084.79
List Price:  ISK kr16,919.19
You save:  ISK kr4,834.40
DH896.11
List Price:  DH1,254.59
You save:  DH358.48
L1,681.36
List Price:  L2,353.97
You save:  L672.61
ден5,231.01
List Price:  ден7,323.62
You save:  ден2,092.61
MOP$805.59
List Price:  MOP$1,127.86
You save:  MOP$322.27
N$1,749.74
List Price:  N$2,449.70
You save:  N$699.96
C$3,666.62
List Price:  C$5,133.41
You save:  C$1,466.79
रु13,774.85
List Price:  रु19,285.34
You save:  रु5,510.49
S/355.16
List Price:  S/497.24
You save:  S/142.08
K418.83
List Price:  K586.38
You save:  K167.55
SAR375.10
List Price:  SAR525.16
You save:  SAR150.05
ZK2,314.30
List Price:  ZK3,240.12
You save:  ZK925.81
L431.52
List Price:  L604.15
You save:  L172.62
Kč2,090.38
List Price:  Kč2,926.62
You save:  Kč836.23
Ft33,856.98
List Price:  Ft47,401.12
You save:  Ft13,544.14
SEK kr952.91
List Price:  SEK kr1,334.11
You save:  SEK kr381.20
ARS$125,835.81
List Price:  ARS$176,175.17
You save:  ARS$50,339.36
Bs688.54
List Price:  Bs963.99
You save:  Bs275.44
COP$406,549.28
List Price:  COP$569,185.25
You save:  COP$162,635.97
₡50,338.53
List Price:  ₡70,475.96
You save:  ₡20,137.42
L2,609.12
List Price:  L3,652.87
You save:  L1,043.75
₲746,361.75
List Price:  ₲1,044,936.31
You save:  ₲298,574.55
$U3,999.43
List Price:  $U5,599.36
You save:  $U1,599.93
zł362.20
List Price:  zł507.10
You save:  zł144.89
Already have an account? Log In

Transcript

Hello, in this Java programming video, we are going to continue from the previous tutorial where we implemented a thread using the runnable interface. And we actually, you know, we implemented that interface, as you can see in the Fred runnable class right here. And we're going to modify this slightly. So we're going to create a thread by extending the thread class. So this is very similar, but it just provides more flexibility when creating multiple threads, and it's the preferred method of doing things. So the changes you'll have to make are pretty minimal, to be honest.

So recommend watching the first tutorial. Instead, we're going to extend the thread like so. We're gonna have you know, a thread we're going to have a name for it. We're gonna have a constructor, still gonna do the same thing like so. gonna print that out, that's fine. Then in the run method, we'll still have the printing intent, we'll have our for loop within the try catch block will still do some sleeping and will still have, you know the interruption handling and in the start method will still check if the thread has been created.

If not, it will create a new thread and you'll do your startup and then start to solve a system Java on it that is it. So if you run it there we go, it has now run so it created the threads, you started them and then it alternates. Obviously, the you know how the actual floor should say be the order of them will vary slightly depending on what else going on in your CPU. There will be various things going on when you run it, but this is the preferred method by extending the thread class. And what I'll do is I'll provide a link With this video, which shows you all of the extra, you know, functionality that you can add to a Fred an extra task, feel free to try and modify the Fred. So you can pass in a integer value.

So you can specify how long you should sleep for. And that way you can modify the sleep for another different Fred. Maybe Fred wanted a higher priority, so you want that to work a lot more. Well, this isn't and also add another task on top of that, once you've done it, feel free to try and create the 10th read the different tasks, different priorities, and maybe you initiate them as well. some pretty cool stuff that you can get started on. Thanks for watching, and I look forward to seeing the next awesome programming video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.